比较给出错误输出的日期 [英] Comparing dates giving incorrect outputs
问题描述
我正在创建一个函数来检查日期的数据库表中哪个日期小于当前日期。像过去一样。
我有3个日期来测试这个函数,其后面的输出:
上个月的日期: 28-04-2015 16:32:00
日期未来: 11-06-2015 13:12:00
上周的日期: 04-05-2015 09:45:00
$ dateNow = date('dmY H:i:s'); // Current
$ deadlineDate =28-04-2015 16:33:18;
if($ deadlineDate< $ dateNow){//如果上个月的日期小于当前日期
echo'< tr class =overdue>'; //过期类给出一个红色背景颜色来标记它
echo'< td>'。$ deadlineDate。'小于'$ dateNow。'< / td>';
} else {
echo'< tr>';
echo'< td>'$ deadlineDate。'是更大的dan'$ dateNow。'< / td>';
}
< / tr>
输出:
28 -04-2015 16:32:00 返回更大的
11-06-2015 13:12:00
04-05-2015 09:45:00
你的日期真的是字符串。当比较它们时,它是按字母顺序排列的。您需要将这些日期转换为实际日期,以使其正常工作:
$ dateNow = new DateTime ();
$ deadlineDate = DateTime :: createFromFormat(d-m-Y H:i:s,28-04-2015 16:33:18);
if($ deadlineDate< $ dateNow){//如果上个月的日期小于当前日期
echo'< tr class =逾期>'; //过期类给出一个红色背景颜色来标记它
echo'< td>'。$ deadlineDate。'小于'$ dateNow。'< / td>';
} else {
echo'< tr>';
echo'< td>'$ deadlineDate。'是更大的dan'$ dateNow。'< / td>';
}
将它们转换为YYYY-MM-DD格式也可以工作。 >
I'm creating a function to check which date, in a database table full of date's, is smaller then then current date. As in the past.
I've got 3 date's to test the function with, and the outputs behind them:
Date from last month: 28-04-2015 16:32:00
Date yet to come: 11-06-2015 13:12:00
Date from last week: 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>
Outputs:
28-04-2015 16:32:00 Returns bigger
11-06-2015 13:12:00 Returns smaller
04-05-2015 09:45:00 Returns smaller
Can anybody tell me what is going wrong?
Your "dates" are really strings. And when comparing them it is alphabetical. You need to convert those dates to real dates for this to work:
$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>';
}
Converting them into YYYY-MM-DD format will also work.
这篇关于比较给出错误输出的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!