简体   繁体   中英

How can I correctly compare two datetimes in my blade file?

I am trying to compare two dates from my blade file, and it works completely fine in my local project and provides correct outcome right by the second, but in the live site, there is a delay in the comparison and it shows the correct result after about one minute. Below is my code in the blade file.

@if (strtotime($timest->format('M-d-y g:i:s')) >strtotime(\carbon\carbon::now('EST5EDT')->format('M-d-y g:i:s')))
    <span class="badge badge-primary">Will Publish At {{ $timest->format('M-d-y g:i:s a') }}</span>
@else
    <span class="badge badge-success">Published AT {{ $timest->format('M-d-y g:i:s a') }}</span>
@endif

You can use Carbon's built in compare methods like so, for a 'greater than' comparison:

@if($timest->gt(Carbon\Carbon::now('EST5EDT'))

Use isFuture or isPast methods from Carbon:

@if ($timest->isFuture())
   Will Publish At {{ $timest->format('M-d-y g:i:s a') }}
@else
   Published AT {{ $timest->format('M-d-y g:i:s a') }}
@endif

If you need to take some advance, you can replace $timest with $timest->addMinutes(2)

But more likely your problem is $timest contains a date based on the user clock instead of the server clock, so they may not be exactly synchronized. You should compare user clock with user clock or server clock with server clock to get a precise comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM