我已经删除了所需的输出和一些样式,因为它们不影响问题。我似乎不能正确地比较两个日期,想法是,如果currentDate大于deadlineDate,将没有输出的路由。我想做的是防止系统列出已经关闭的路线。我不明白为什么这么难,或者我错过了一些非常基本的东西。
<?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时间戳格式,如果我理解正确的话,这正是我在这里要做的。我敢肯定我的错误很简单。
奇怪的是,对于某些日期,如果截止日期超过一个月,它似乎有效。在d.m.y G:i
格式中正确填写deadlineDate。
我跳过格式化和比较strtotimes
我说:
$currentDate2=strtotime("now");
$deadlineDate2 = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
然后比较$currentDate2
和$deadlineDate2
跳过格式化,只比较strtotime()
的结果。