user14232549
user14232549

Reputation: 413

I am looking for a way to compare months in PHP

How should I compare months of two date data?

Is there any a good way? My approach gave me

ErrorException A non well formed numeric value encountered

I tried this to solve this. I would like to get a date with strtotime. last fri or sat or sun on a month

$last_Date = date("Y-m-d", strtotime("last fri of  next month", strtotime($request->deli_date)));
$last_Date_add1day = date("Y-m-d", strtotime("last fri of next month +1 day", strtotime($request->deli_date)));
if(date("n", $last_Date) == date("n", $last_Date_add1day)){
    $last_Date_add2day = date("Y-m-d", strtotime("last fri of next month +2 day", strtotime($request->deli_date)));
    if(date("n", $last_Date_add1day) == date("n",$last_Date_add2day)){
        $last_Date = $last_Date_add2day;
    }else{
        $last_Date = $last_Date_add1day;
    }
}

Upvotes: 0

Views: 45

Answers (1)

Justinas
Justinas

Reputation: 43479

Drop date('Y-m-d') from anywhere where you use strtotime. It converts timestamp (int) to formatted date (string) and you feed it into parameter that requires INT

$last_Date = strtotime("last fri of  next month", strtotime($request->deli_date));
$last_Date_add1day = strtotime("last fri of next month +1 day", strtotime($request->deli_date));

if(date("n", $last_Date) == date("n", $last_Date_add1day))
{
    $last_Date_add2day = strtotime("last fri of next month +2 day", strtotime($request->deli_date));
    if(date("n", $last_Date_add1day) == date("n", $last_Date_add2day)){
        $last_Date = $last_Date_add2day;
    }
    else
    {
        $last_Date = $last_Date_add1day;
    }
}

Example

Upvotes: 2

Related Questions