我正在开发一个 php 应用程序,需要一些帮助来比较日期。 我有一个日期选择输入字段 (datepicker),由于客户端代码,它总是以以下格式发布日期:mm/dd/YYYY,例如 02/25/2015。
我想做的是使用 php 断言此日期不晚于当前日期。
最初我将本地时区设置为:
date_default_timezone_set('Europe/London');
在我的代码中:
} elseif (date(strtotime($_POST['datepicker'])) > date('m,d,Y')){
$displayblock.= "<br><p>The selected date is in the future!!! </p>".date("d/m/Y", strtotime($_POST['datepicker']));
$alertbox = "<script>alert('".$_POST['datepicker']." is in the future!!! Shall we try that again? :-)');</script>";
这显然远非优雅,而且似乎也没有正确比较日期。 有人可以帮忙吗?
非常感谢,
最佳答案
如果它们以字符串的形式出现在这个函数中,您也可以比较它们。第一个日期是来自 html/javascript 日期选择器的日期,第二个日期是服务器的实际日期:
function stringDateIsAPastDate($datestring1){
if ((date("j-m-Y", strtotime($datestring1))) <= (date("j-m-Y"))){
//here the code to do when the date inserted from the website is a past date or today
return(true);
}else{
return(false);
}
}
关于php - 在 PHP 中比较日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718934/