【发布时间】:2017-03-25 16:05:43
【问题描述】:
我正在开发一个 PHP 程序,我必须比较一个 date 变量和一个 string variable。
我试过strcmp,但它不起作用......
建议?
谢谢
【问题讨论】:
-
字符串变量是日期吗?
我正在开发一个 PHP 程序,我必须比较一个 date 变量和一个 string variable。
我试过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 是整数,所以所有适用于整数的东西都适用于那些。
===,因为它比== 更快,更合适。如果对您有帮助,请将答案标记为已接受。
$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
【讨论】: