Home > Net >  How to properly compare two Unix timestamps
How to properly compare two Unix timestamps

Time:11-09

I'm working with Laravel 5.8 and I wanted to show a popup message if the unix timestamp of the current date is equals to the defined unix timestamp of the popup.

So in order to do that, I added this at the Controller:

if($popup->datep == now()->timestamp){
    $output .=' <a href=" '.$popup->linkp.' "><img src=" '. URL::to('popups/'.$popup->image_path).' " style="width: 100%;"></a>';
}

And now the $popup->datep shows 1636403400 and now()->timestamp shows 1636355812.

But the comparison of them always returns TRUE and shows the popup message. Even I enter a timestamp of next year!

So what's going wrong here? How can I properly compare two Unix timestamps together and show some results based on that?

CodePudding user response:

$date1= $popup->datep;
$date2 = Carbon::now;

$date1 = Carbon::createFromFormat('Y-m-d H:i:s', $date1);
$date2 = Carbon::createFromFormat('Y-m-d H:i:s', $date2);

$result = $date1->eq($date2);

if($result === true){
  dd("equal");
}

you need to take date and format same and then you can you conditional.

  • Related