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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?