我已经删除了所需的输出和一些样式,因为它们不会影响问题。我似乎无法正确比较这两个日期,想法是,如果currentDate大于deadlineDate,则该路线将不会有输出。我试图做的是防止系统列出已经关闭的路线。我不明白为什么它如此困难,或者我错过了一些非常基本的东西。为什么我在PHP中比较两个日期不起作用?
<?php
$driveDays = mysql_query("SELECT date,routeid from StopDates where routeid='".$row['id']."' ORDER BY date ASC");
while($stopDates = mysql_fetch_array($driveDays)){
$orderDaysBefore = $row['lastOrderDate']; // How many days before the order must be placed.
// Change the date taken from the query to new format
$originalDate=($stopDates['date']);
$newDate = date("d.m", strtotime($originalDate));
// Count the deadline date for the route.
$deadlineDate = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
$deadlineDate = Date('d.m.y G:i', $deadlineDate);
//Get current date which is then compared to the deadline date. Idea is that if currentDate is larger than deadlinedate there will be no input.
$currentDate=Date("d.m.y G:i");
//The line below doesnt seem to be working, i have tried mktime and time too but for some reason it just cant compare.
if (strtotime($currentDate) > strtotime($deadlineDate)){
// Output nothing
}
else { ?>
<p>Output stuff here</p>
<?php
}
}
?>
问题是,对于某些行,它隐藏了路线,而对于一些行却没有。我试图用mktime和time来做到这一点,但我似乎无法弄清楚问题所在。我看到的大多数指南都告诉将日期转换为unix时间戳格式,如果我正确理解那就是我正在尝试在这里执行的操作。林肯定我的错误是一个简单的。
奇怪的是,在某些日期,它似乎工作,如果deadlineDate超过一个月大。截止日期格式正确格式为d.m.y G:i
。
解决
我跳过格式相比strtotimes
我说:
$currentDate2=strtotime("now");
$deadlineDate2 = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
然后我比较$currentDate2
和$deadlineDate2
'lastOrderDate'不在您的查询中作为字段返回... –
您正在做太多的字符串和时间戳格式之间来回转换。 >。<我很确定某些格式转换的方式正在搞砸...... – deceze
@Jared Farrish'lastOrderDate'来自不同的查询,值是正确的。 – kartaju