[wd_asp elements=’search’ ratio=’100%’ id=1]

Compare Year and Month date only in PHP

7th August 2019

Php - Dates & Time

php codehaven

This was a difficult one to get right, comparing the Month and Year only was full of surprises.
My variable $joined was “2019-12-01 00:00:00”
and my Month was “Jul” and my Year was “2019”
Just cut and paste and your done!


$month="Jul";
$year="2019";
$mydate="2018-12-01 00:00:00";

$joined = date("Ym", strtotime($mydate));

$askedfor = $month." ".$year;
$askedfor = date("Ym", strtotime($askedfor));

if ($askedfor > $joined) {
echo "yes";
}
else {
echo "no";
}