doudaochu1699 2018-04-15 02:42
浏览 135
已采纳

php命令由created_at,午夜永远是第一位的

I have a problem with sorting values on my table. I sorted these items by created_at column. Its all good but when the table receives an item on 12:01am to 12:59am, they always appear first in the lists. Im am using custom date format.

 $date_time = @date('F j Y g:i a', strtotime(@date('F j Y g:i a')));

and has an output of "April 15 2018 2:07 am"

my created_at column is varchar and the reason for that is I'm using date_default_timezone_set(); because the web server store item in different timezone and ruins the datetime when the item was actually created, that is why i dont use timestamp.

one more thing is that i want these time rendered as posted 3 hours ago or yesterday whatever the case maybe.

Im using laravel 5.6 for this one.

  • 写回答

1条回答 默认 最新

  • duanbai5348 2018-04-15 05:32
    关注

    For the date time formatting while rendering you can create a mutator in your model as:

    public function getCreatedAtAttribute($value)
    {
        return Carbon\Carbon::createFromFormat('F j Y g:i a', $value)->diffForHumans();
    }
    

    For sorting, what are you trying to achieve? If you are sorting the dates then I midnight comes first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: