【发布时间】:2012-12-18 04:01:52
【问题描述】:
我有一个日期验证器代码段,它应该验证任何未来的日期。出于调试目的,我希望它在明天或大约 3 小时内到期。截至撰写本文时,明天是 12 月 18 日。它的 10:40 PM GMT-5 Dec 17 它将其标记为过去。使用 3 个发布变量输入日期:日、月、年并连接起来,附加“01:01:59”,并作为时间戳插入到表中,并由脚本定期检查项目是否已过期。下面的代码不会验证任何距离 2 天以内的日期。我不知道为什么。
代码:
if(check_post('day')&&check_post('month')&&check_post('year'))
{
//die("day: ".$_POST['day']." month: ".$_POST['month']." year: ".$_POST['year']);
if(!check_post('day',"Select Day")&&!check_post('month',"Select Month")&&!check_post('year',"Select Year"))
{
$days = array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31");
$today = explode("-",date("d-m-Y"));
if(checkdate($_POST['month'],$_POST['day'],$_POST['year']))
{
$c_y = ($_POST['year']==$today[2]);
$c_m = ($_POST['month']==$today[1]);
$p_d = ($today[0]>$_POST['day']);
$p_m = ($today[1]>$_POST['month']);
if(!($c_y&&(($c_m&&$p_d)||$p_m)))
{
$_POST['date']=strval($_POST['year'])."-".(($_POST['month']>9)?strval($_POST['month']):"0".strval($_POST['month']))."-".(($_POST['day']>9)?strval($_POST['day']):"0".strval($_POST['day']));
$_POST['date'] .= " 01:01:59";
//$_POST['date'] = mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year']);
//die($_POST['date']);
}
else
{
add_error("Date must be current");
}
}
else
{
add_error("Invalid expiration date");
}
}
else
{
add_error("Pick an expiration date");
}
}
else
{
add_error("Date not set");
}
没有什么功能可以为我做这件事吗?
【问题讨论】:
-
php.net/DateTime php.net/DateInterval - 就 PHP4 而言,我投票结束这个问题,因为它过于本地化。
-
即使在 PHP4 中,
strtotime也很重要。另请在此处插入关于 PHP4 已过时且可能不安全的讲座,以及不应如何使用它以及仍然允许选择它的任何网络托管服务提供商如何是您不想与之开展业务的公司. -
谢谢你,查尔斯,我忘了 strtotime。如果您将其发布为答案,我可以接受。我似乎错过了很多。