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

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?

图片转代码服务由CSDN问答提供 功能建议

我在数据库中将日期存储为VARCHAR时犯了一个错误,这里看起来像 02 / 12/2018 我创建了一个使用碳的变量来获取当前日期 +14天

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

问题是

我正在尝试 比较这个日期 02/12/2018 ,它在数据库中并存储为VARCHAR到我使用Carbon生成的日期 09/04/2017

Eloquent Code

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

我得到了什么

它没有得到任何结果,因为它比较 只有 required_date 中的日期和碳日期中的日期。

虽然它应该返回值,因为它超过了 1年的差异?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题