douweng7233 2018-03-08 01:38
浏览 80

奇怪的DateTime错误?

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();
  • 写回答

1条回答 默认 最新

  • douxuanou2787 2018-03-08 02:35
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统