我的if语句存在问题,因为这两种情况都分别给出了预期的结果,但是如果组合使用,那么当两个条件都成立时,该语句就会中断。
这个想法是要计算圣诞节前的开始日期,以便我可以显示有关此消息。我的逻辑是,通过确保开始日期在10月1日之后,结束日期在12月24日之前,此消息将始终在正确的时间出现,并提供我们的开始日期和结束日期时间表。
到目前为止,我的代码在下面,每个变量下面都有一个转储。
foreach ( $events as $event ) {
$start = date("d-m-Y", strtotime($event->EventStartDate));
// string(10) "26-10-2018"
$end = date("d-m-Y", strtotime($event->EventEndDate));
// string(10) "20-12-2018"
echo '<div id="prod-dates">';
echo '<p>Order before ' . $start . '</p>';
echo '<p>Estimated Delivery ' . $end . '</p>';
echo '</div>';
//
//EVERYTHING IS FINE UNTIL HERE...
//
$this_christmas = date('Y-12-24');
// string(10) "2018-12-24"
$startx = date("Y-m-d", strtotime($event->EventStartDate));
// string(10) "2018-10-26"
$endx = date("Y-m-d", strtotime($event->EventEndDate));
// string(10) "2018-12-20"
$note_start = date('Y-11-01');
// string(10) "2018-11-01
if ( $startx >= $note_start || $endx <= $this_christmas ) {
echo 'This is your last deadline for <span>Christmas Delivery</span>';
}
}
任何帮助表示赞赏。
谢谢
答案 0 :(得分:1)
date函数返回一个字符串值,因此将您的日期作为字符串进行比较。
最好使用返回 strtotime 的值来比较您的日期
示例:
$start = strtotime($event->EventStartDate);
$end = strtotime($event->EventEndDate);
//$note_start in time format
if ( $startx >= $note_start || $endx <= $this_christmas ) {
//echo
}
答案 1 :(得分:1)
您在描述中说“ 10月1日”,但代码$note_start
设置为11月(2018-11-01)。
您要确保“开始日期在10月1日之后,结束日期在12月24日之前”,但是您要检查{em> em 的条件是否为if ( $startx >= $note_start || $endx <= $this_christmas )
。将<或(||
)替换为和(&&
)。