php中的存储日期和系统日期比较
我将存储日期设置为dd-mm-yy,并按日期(d.m.y)获取系统日期。 所以我必须比较这两个值,找出哪一个更大。 如果更改结束日期,以下代码将无法正常工作。我还需要在Javascript中进行比较。 这是我写的php中的存储日期和系统日期比较,php,Php,我将存储日期设置为dd-mm-yy,并按日期(d.m.y)获取系统日期。 所以我必须比较这两个值,找出哪一个更大。 如果更改结束日期,以下代码将无法正常工作。我还需要在Javascript中进行比较。 这是我写的 $day=date("d"); $month=date("m"); $year=date("y"); $year="20".$year; $enddate='31-11-2011'; $end=explode("-",$enddate); $end_day=$end[0]; $end
$day=date("d");
$month=date("m");
$year=date("y");
$year="20".$year;
$enddate='31-11-2011';
$end=explode("-",$enddate);
$end_day=$end[0];
$end_month=$end[1];
$end_year=$end[2];
$allow=0;
if($year<=$end_year)
{
if($month<=$end_month)
{
if($day<=$end_day)
{
$allow=1;
}
else
{
$allow=0;
}
}
else
{
$allow=0;
}
}
else
{
$allow=0;
}
if($allow==0)
{
echo "expired";
}
else
{
echo "Active";
}
$day=日期(“d”);
$month=日期(“m”);
$year=日期(“y”);
$year=“20”。$year;
$enddate='2011年11月31日';
$end=分解(“-”,$enddate);
$end_day=$end[0];
$end_month=$end[1];
$end_year=$end[2];
$allow=0;
如果($year尝试这样做:
$today = strtotime(date("d") . "/" . date("m") . "/20" . date("y") . " 00:00:00");
$toCompare = strtotime("31/11/2011 00:00:00");
if($today == $toCompare)
{
//Same
}
else
{
//Different
}
使用的两个函数是and time()。比较两个日期有一种更简单的方法:
$currentDate = Date ( 'm-d-Y' ); // Y will return year in XXXX format not like y which returns year in XX format
$endDate = '31-11-2011';
if ( StrToTime ( $currentDate ) > StrToTime ( $endDate ) ) {
// current date is bigger
} else {
// end date is bigger
}
该函数将日期的字符串表示形式转换为一个整数UNIX时间戳。您的解决方案在每天00:00:00Ah时仅在1秒内有效。我没有意识到他只是想知道日期是否相同。现在将编辑我的答案。谢谢,它可以工作,您能建议我使用javascript时使用哪个函数l我用这里有一个用于strototime的Javascript函数:谢谢你,它可以工作,如果是Javascript,你能建议我使用哪个函数吗