user9457459
user9457459

Reputation:

Weird DateTime bug?

I have a weird issue with datetimes in my database and PHP code. Carbon::now() currently returns 2018-03-08 01:33:47 which is correct, that is the time on my computer and in my country.

I put a record with the expires_at value at 2018-03-08 02:32:02 which is in 1 hour (or 59 minutes of you want to become technical).

It wasn't displayed in my query, the query that is supposed to select all records where expires_at is before the current time, so non expired records.

Althought, when setting the datetime to 2018-03-09 01:32:28 it displays that it expires in 23 hours? If I post it anything older (say 14 hours after the current datetime) it will be shown as expired?

$keys = UserAccountKey::where('user_id', Auth::user()->id)
    ->whereDate('expires_at', '>=', Carbon::now())
    ->get();

Upvotes: 0

Views: 62

Answers (1)

Song
Song

Reputation: 301

The sql of your query is:

select * from `user_account_keys` where date(`expires_at`) >= '2018-03-08 01:33:47'

The date('expires_at') gets the date part of the expires_at field, ie '2018-03-08', obviously '2018-03-08' is less than '2018-03-08 01:33:47', so Your query did not get any result.

Just use where('expires_at', '>=', Carbon::now()) instead.

See this demo https://implode.io/vTWTWP

Upvotes: 1

Related Questions