-10

我在这里验证我在 PHP 上的 $input 的时间时遇到问题。我的输入值为

yy-mm-dd hh:mm PM/AM

如果选择的日期是过去或今天,则会出现错误。我怎样才能做到这一点?

任何帮助都是一种感激。谢谢。

4

4 回答 4

5

strtotime是你的朋友。

$input = '2012-06-07 01:23 PM';
if(strtotime($input) < time()){
    echo "error";
}
于 2012-06-27T16:47:22.110 回答
3

您说的是今天,所以我假设您指的不是当前时间,而是当前日期?

<?php
$input = strtotime( '2012-06-07 01:23 PM' );

if ( date( 'Y-m-d', $input ) == date( 'Y-m-d' ) || $input > time() ) {
    echo 'ERROR';
}
于 2012-06-27T16:50:26.870 回答
0
$today = strtotime($todays_date);
$expiration_date = strtotime($input);
if ($expiration_date > $today) {
 #你要执行的代码放在这里
}

或使用日期时间

$d1 = new DateTime("now");
$d2 = 新日期时间($input);
#使用PHP的关系运算符比较两者。
于 2012-06-27T16:54:06.917 回答
0

在 PHP 中,您可以使用以下方法进行检查:

$input_date_time = strtotime( 'your input date here' );
$input_date = strtotime(date( 'Y-m-d', $input_date_time ));
$current_date = strtotime(date( 'Y-m-d',time()));

if ($input_date_time < time () || $input_date == $current_date) {
       echo "error here";
}

您还可以使用 jQuery 日期时间选择器。如果您使用 jQuery 日期时间选择器,则可以选择阻止选择当前日期之前的日期或您想要的日期。

希望这可以帮助

于 2012-06-27T16:54:32.347 回答