我可以通过获取当前日期()突出显示这一天。如何在两次(开放时间)之间突出显示。
<?php $day = date('l'); ?>
<div<?php if ($day == "Monday") { echo ' class="highlight"'; } ?>>Monday: 9am - 6pm</div>
<div<?php if ($day == "Tuesday") { echo ' class="highlight"'; } ?>>Tuesday: 9am - 6pm</div>
<div<?php if ($day == "Wednesday") { echo ' class="highlight"'; } ?>>Wednesday: 9am - 6pm</div>
<div<?php if ($day == "Thursday") { echo ' class="highlight"'; } ?>>Thursday: 9am - 6pm</div>
<div<?php if ($day == "Friday") { echo ' class="highlight"'; } ?>>Friday: 9am - 6pm</div>
<div<?php if ($day == "Saturday") { echo ' class="highlight"'; } ?>>Saturday: 9am - 4pm</div>
<div<?php if ($day == "Sunday") { echo ' class="highlight"'; } ?>>Sunday: 9am - 4pm</div>
我想了一个像下面这样的方式,但这也不正确,我也强调了几个小时之间。
<?php $day = date('l'); ?>
<?php $time = date('h:i'); ?>
<div<?php if ($day == "Monday" && ($time <= "9:00" || $time >= "6:00") { echo ' class="highlight"'; } ?>>Monday: 9am - 6pm</div>
很乐意寻求帮助。
答案 0 :(得分:1)
请记住,24小时制更容易使用:
<?php
$time = date('G');
$date = date('N');
?>
<div<?php if ($day === "1" && $time >= 9 && $time <= 18) { echo ' class="highlight"'; } ?>>Monday: 9am - 6pm</div>
<div<?php if ($day === "2" && $time >= 9 && $time <= 18) { echo ' class="highlight"'; } ?>>Tuesday: 9am - 6pm</div>
<div<?php if ($day === "3" && $time >= 9 && $time <= 18) { echo ' class="highlight"'; } ?>>Wednesday: 9am - 6pm</div>
etc...
如果将开放时间数据组织到一个数组中并循环生成数组以产生所需的输出,可能会更容易。
答案 1 :(得分:1)
以下是使用DateTime()
的示例。它使代码更清晰。 (我使用DateTimeImmutable()
只能在PHP 5.5+中使用。如果您使用的是旧版本的PHP,请告诉我,我可以为该版本工作。)
<?php
$now = new DateTimeImmutable();
$day = $now->format('l');
$open = $now->setTime(9, 0);
$close = $now->setTime(18, 0);
?>
<div<?php if ($day == "Monday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Monday: 9am - 6pm</div>
<div<?php if ($day == "Tuesday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Tuesday: 9am - 6pm</div>
<div<?php if ($day == "Wednesday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Wednesday: 9am - 6pm</div>
<div<?php if ($day == "Thursday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Thursday: 9am - 6pm</div>
<div<?php if ($day == "Friday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Friday: 9am - 6pm</div>
<?php
$close = $now->setTime(16, 0);
?>
<div<?php if ($day == "Saturday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Saturday: 9am - 4pm</div>
<div<?php if ($day == "Sunday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Sunday: 9am - 4pm</div>
这是PHP 5.3版本
<?php
$now = new DateTime();
$day = $now->format('l');
$open = new DateTime();
$open->setTime(9, 0);
$close = new DateTime();
$close->setTime(18, 0);
?>
<div<?php if ($day == "Monday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Monday: 9am - 6pm</div>
<div<?php if ($day == "Tuesday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Tuesday: 9am - 6pm</div>
<div<?php if ($day == "Wednesday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Wednesday: 9am - 6pm</div>
<div<?php if ($day == "Thursday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Thursday: 9am - 6pm</div>
<div<?php if ($day == "Friday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Friday: 9am - 6pm</div>
<?php
$close->setTime(16, 0);
?>
<div<?php if ($day == "Saturday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Saturday: 9am - 4pm</div>
<div<?php if ($day == "Sunday" && $now > $open && $now < $close) { echo ' class="highlight"'; } ?>>Sunday: 9am - 4pm</div>
答案 2 :(得分:1)
我认为你不能将这样的字符串与大于等的字符串进行比较。
只用小时尝试相同
<?php $day = date('l'); ?>
<?php $time = (int) date('H'); ?>
<div<?php if ($day == "Monday" && ($time >= 9 || $time < 18)) { echo ' class="highlight"'; } ?>>Monday: 9am - 6pm</div>
答案 3 :(得分:0)
试试这个
$opentime = "9:00 am";
$closetime = "6:00 pm";
$day = date('l');
$time = date('h:i');
$open = DateTime::createFromFormat('H:i a', $opentime);
$close = DateTime::createFromFormat('H:i a', $closetime);
if ($day == "Monday" && $time > $open && $time < $close) {
echo 'Monday: 9am - 6pm';
}
etc........