简体   繁体   English

如何在 php laravel 中检查日期是否比 3 个月大

[英]how to check if date is 3 months older in php laravel

I have a date in this variable我在这个变量中有一个日期

$start_date = '2021-03-04';

I want to check if this date is three months older than current date How i can check that我想检查这个日期是否比当前日期早三个月我如何检查

if($start_date < 3 months ???)
{

}

You can use Carbon to achive this:您可以使用Carbon来实现:

$start_date = new Carbon\Carbon('2021-03-04');
if($start_date->diffInMonths() > 3)
{

}

You need something similar to:你需要类似的东西:

$format = "y-m-d";
$start_date = \DateTime::createFromFormat($format, $start_date);
$end_date = \DateTime::createFromFormat($format, $start_date);
$end_date = $end_date->modify('+3 month');
if($start_date < $end_date)
{

}

I haven't tested the code, but you can get an idea.我还没有测试代码,但你可以得到一个想法。

You can use this :你可以使用这个:

$start_date = '2021-03-04';

if($start_date < date('Y-m-d', strtotime('-3 months'))){
    
}

You can use Carbon for that.你可以使用碳来做到这一点。

        // parse your date as carbon date & only take the date string using toDateString
        $start_date = Carbon::parse('2021-03-04')->toDateString();

        // take the Today time, subtract 3 months from it & only take date string from it
        $today = Carbon::today()->subMonths(3)->toDateString();
        if ($start_date < $today) {
            // whatever you wanna do here
        }

Note: Carbon adds timestamp so using toDateString() you ignore that timestamp & only factor in the date string注意:Carbon 添加时间戳,因此使用toDateString()您可以忽略该时间戳并仅考虑日期字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM