比较日期给出不正确的输出
Comparing dates giving incorrect outputs
我正在创建一个函数来检查在充满日期的数据库 table 中哪个日期小于当前日期。一如既往
我有 3 个日期来测试函数,以及它们背后的输出:
上个月的日期:28-04-2015 16:32:00
日期尚未到来:11-06-2015 13:12:00
上周日期:04-05-2015 09:45:00
$dateNow = date('d-m-Y H:i:s'); //Current
$deadlineDate = "28-04-2015 16:33:18";
if($deadlineDate < $dateNow){ //If date from last month is smaller then the current date
echo '<tr class="overdue">'; //Overdue class gives that tr an red background color to mark it
echo '<td>'.$deadlineDate.' is smaller then '.$dateNow.'</td>';
}else{
echo '<tr>';
echo '<td>'.$deadlineDate.' is bigger dan '.$dateNow.'</td>';
}
</tr>
输出:
28-04-2015 16:32:00 Returns 更大
11-06-2015 13:12:00 Returns 更小
04-05-2015 09:45:00 Returns 更小
谁能告诉我哪里出了问题?
你不能这样比较 date string
。首先将其转换为 timestamp
。使用 strtotime
.
$dateNow = date('d-m-Y H:i:s'); //Current
$deadlineDate = "28-04-2015 16:33:18";
if(strtotime($deadlineDate) < strtotime($dateNow)){ //If date from last month is smaller then the current date
echo '<tr class="overdue">'; //Overdue class gives that tr an red background color to mark it
echo '<td>'.$deadlineDate.' is smaller then '.$dateNow.'</td>';
}else{
echo '<tr>';
echo '<td>'.$deadlineDate.' is bigger dan '.$dateNow.'</td>';
}
你的 "dates" 确实是字符串。当比较它们时,它是按字母顺序排列的。您需要将这些日期转换为 真实 日期才能正常工作:
$dateNow = new DateTime();
$deadlineDate = DateTime::createFromFormat("d-m-Y H:i:s", "28-04-2015 16:33:18");
if($deadlineDate < $dateNow){ //If date from last month is smaller then the current date
echo '<tr class="overdue">'; //Overdue class gives that tr an red background color to mark it
echo '<td>'.$deadlineDate.' is smaller then '.$dateNow.'</td>';
}else{
echo '<tr>';
echo '<td>'.$deadlineDate.' is bigger dan '.$dateNow.'</td>';
}
将它们转换为 YYYY-MM-DD 格式也可以。
我正在创建一个函数来检查在充满日期的数据库 table 中哪个日期小于当前日期。一如既往
我有 3 个日期来测试函数,以及它们背后的输出:
上个月的日期:28-04-2015 16:32:00
日期尚未到来:11-06-2015 13:12:00
上周日期:04-05-2015 09:45:00
$dateNow = date('d-m-Y H:i:s'); //Current
$deadlineDate = "28-04-2015 16:33:18";
if($deadlineDate < $dateNow){ //If date from last month is smaller then the current date
echo '<tr class="overdue">'; //Overdue class gives that tr an red background color to mark it
echo '<td>'.$deadlineDate.' is smaller then '.$dateNow.'</td>';
}else{
echo '<tr>';
echo '<td>'.$deadlineDate.' is bigger dan '.$dateNow.'</td>';
}
</tr>
输出:
28-04-2015 16:32:00 Returns 更大
11-06-2015 13:12:00 Returns 更小
04-05-2015 09:45:00 Returns 更小
谁能告诉我哪里出了问题?
你不能这样比较 date string
。首先将其转换为 timestamp
。使用 strtotime
.
$dateNow = date('d-m-Y H:i:s'); //Current
$deadlineDate = "28-04-2015 16:33:18";
if(strtotime($deadlineDate) < strtotime($dateNow)){ //If date from last month is smaller then the current date
echo '<tr class="overdue">'; //Overdue class gives that tr an red background color to mark it
echo '<td>'.$deadlineDate.' is smaller then '.$dateNow.'</td>';
}else{
echo '<tr>';
echo '<td>'.$deadlineDate.' is bigger dan '.$dateNow.'</td>';
}
你的 "dates" 确实是字符串。当比较它们时,它是按字母顺序排列的。您需要将这些日期转换为 真实 日期才能正常工作:
$dateNow = new DateTime();
$deadlineDate = DateTime::createFromFormat("d-m-Y H:i:s", "28-04-2015 16:33:18");
if($deadlineDate < $dateNow){ //If date from last month is smaller then the current date
echo '<tr class="overdue">'; //Overdue class gives that tr an red background color to mark it
echo '<td>'.$deadlineDate.' is smaller then '.$dateNow.'</td>';
}else{
echo '<tr>';
echo '<td>'.$deadlineDate.' is bigger dan '.$dateNow.'</td>';
}
将它们转换为 YYYY-MM-DD 格式也可以。