我有一个if语句,它将输入日期与系统日期进行比较。
$Date > date('m/d/Y')
问题是当我输入12/16/2012
时,它会抛出一个错误,指出日期大于今天的日期。我不知道我的 if 语句中的问题是什么。我在该函数中有一个try
和catch
,它将捕获任何输入异常。
比较字符串不会给你所需的输出。您可以使用strtotime
来比较日期,如下所示: strtotime($date) > date('m/d/Y',time())
strtotime($date) > strtotime(date('m/d/Y'));
使用 strtotime()
解析存储在 $Date
中的值以获取时间戳(即自 1970 年 1 月 1 日以来经过的秒数)。然后,将其与当前时间戳进行比较,使用函数 time()
if (strototime($Date) > time()) {
echo($Date.' is in the future.');
} else {
echo($Date.' is in the past.');
}