Compare given date with today

前端 未结 13 1994
别那么骄傲
别那么骄傲 2020-11-22 16:22

I have following

$var = \"2010-01-21 00:00:00.0\"

I\'d like to compare this date against today\'s date (i.e. I\'d like to know if this

13条回答
  •  有刺的猬
    2020-11-22 17:02

    Expanding on Josua's answer from w3schools:

    //create objects for the dates to compare
    $date1=date_create($someDate);
    $date2=date_create(date("Y-m-d"));
    $diff=date_diff($date1,$date2);
    //now convert the $diff object to type integer
    $intDiff = $diff->format("%R%a");
    $intDiff = intval($intDiff);
    //now compare the two dates
    if ($intDiff > 0)  {echo '$date1 is in the past';}
    else {echo 'date1 is today or in the future';}
    

    I hope this helps. My first post on stackoverflow!

提交回复
热议问题