How to compare dates and strings in PHP(如何在 PHP 中比较日期和字符串)
问题描述
我正在开发一个 PHP 程序,我必须比较 date
变量和 string 变量
.我试过 strcmp
但它不起作用......建议?谢谢
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
推荐答案
比较日期的最佳方法是使用时间戳:
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
}
希望对你有帮助
这篇关于如何在 PHP 中比较日期和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!