How to compare dates and strings in PHP

拜拜、爱过 提交于 2019-12-19 17:33:14

问题


i'm developing a PHP program and i must compare a date variable and a string variable. I've tried strcmp but it doesn't work... suggests? Thank's


回答1:


Best way of comparing dates is using time-stamps :

$string = '11/05/2016';//string variable
$date = date('Y-m-d',time());//date variable

$time1 = strtotime($string);
$time2 = strtotime($date);
if($time1>$time2){
    //do this
}
else{
    //do this
}

I hope it helps




回答2:


$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time); //my date format is Y-m-d,usw your date format

there $newformat variable also a date

this way you can compare date and string



来源:https://stackoverflow.com/questions/40547752/how-to-compare-dates-and-strings-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!