Search code examples
phplaravellaravel-5php-carbon

can't validate true or false var_dump in laravel carbon


I write code to check current time between 2 times $start and $end like this:

$current = new Carbon();
$start = product::select('dateS')->where('id',$req->id)->first(); 
$end = product::select('dateE')->where('id',$req->id)->first();
$val= var_dump($current->between($start->dateS,  $end->dateE));

It work and when i use dd($val) it will show:

bool(false) null or bool(true) null

Next, i want to do like this but it only returns else case. What did I do wrong?

 if($val == true){
      echo "current time is between start and end";
    }
    else{
      echo "current time isn't between start and end";
    }

Solution

  • You can get start and end as below:

    $start_and_end_time = product::where('id',$req->id)->select(['dateS', 'dateE'])->first();
    

    If you have set date cast to dateS and dateE in your Product model, you'll get carbon instances by the above query.

    Product model

    dates = [
        'dateS',
        'dateE'
    ];
    

    So, you can compare current datetime with the dateS and dateE as below:

    $val = \Carbon::now()->between($start_and_end_time->dateS, $start_and_end_time->dateE);