我正在开发一个PHP程序,需要比较一个日期变量
和一个字符串变量
。
我尝试了strcmp
,但它不起作用...
有什么建议吗?
谢谢
比较日期的最好方法是使用时间戳:
$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
}
希望能对你有所帮助
$time1
和$time2
都是整数,因此所有适用于整数的操作都可以在它们上面执行。 - Abhay Maurya===
代替==
,因为它比==
更快且更合适。如果这个答案对您有帮助,请将其标记为已接受的答案。 - Abhay Maurya$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time); //my date format is Y-m-d,usw your date format
有一个名为$newformat
的变量,它也是一个date
。
这样你就可以比较date
和string
了。