Check if timestamp is today

后端 未结 7 949
傲寒
傲寒 2020-12-29 19:28

I\'ve got a timestamp in the following format (Which can easily be changed thanks to the beauties of PHP!).

2011-02-12 14:44:00

7条回答
  •  执笔经年
    2020-12-29 19:53

    This is what i use for this kind of task :

    /** date comparator restricted by $format.
     @param {int/string/Datetime} $timeA
     @param {int/string/Datetime} $timeB
     @param {string} $format
     @returns : 0 if same. 1 if $timeA before $timeB. -1 if after   */
    function compareDates($timeA,$timeB,$format){
        $dateA=$timeA instanceof Datetime?$timeA:(is_numeric($timeA)?(new \Datetime())->setTimestamp($timeA):(new \Datetime("".$timeA)));
        $dateB=$timeB instanceof Datetime?$timeB:(is_numeric($timeB)?(new \Datetime())->setTimestamp($timeB):(new \Datetime("".$timeB)));
        return $dateA->format($format)==$dateB->format($format)?0:($dateA->getTimestamp()<$dateB->getTimestamp()?1:-1);
    }
    

    compare day : $format='Y-m-d'.
    compare month : $format='Y-m'.
    etc...

    in your case :

    if(compareDates("now",'2011-02-12 14:44:00','Y-m-d')===0){
        // do stuff
    }
    

提交回复
热议问题