简体   繁体   中英

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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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