dougou8458 2017-03-25 21:37
浏览 307
已采纳

Laravel将数据库中的字符串日期与碳日期进行比较

I have made a mistake by storing the date as VARCHAR in the database and here it looks like 02/12/2018 and I have created a variable using carbon to get the current date +14 days.

$current_date_plus_14 = Carbon::now() -> addDay(14) -> format('d/m/Y');

Problem is

I am trying to compare this date 02/12/2018 which is in the database and stores as VARCHAR to the date that I have generated using Carbon which is 09/04/2017.

Eloquent Code

$gquery = Client::where('required_date', '>=', $current_date_plus_14) -> get();

What I get

It doesn't get any results because it compare only the day in the required_date and the day in the carbon date.

While it should return value because the there is more than 1 year difference?

  • 写回答

2条回答 默认 最新

  • doushi6932 2017-03-25 21:44
    关注

    Try this function that converts date as String (May work with varchar) to a real date

    private function convertDateString($date)
    {
        if (is_string($date)) {
            $date = Carbon::parse($date,new DateTimeZone('YOUR_DATE_TIME_ZONE'));
        }
    
        return $date;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘