我想在php的下拉菜单中更新我的日期在每周三中午12:00,并保持不变,直到下周三。
下面是我的代码:$now = time(); // current timestamp
$today = date("w", $now); // "w" returns the weekday (number)
$wednesday = 3; // 5th day of the week (sunday = 0)
if ($today == $wednesday) {
$ts = $now; // today is wednesday, don't change the timestamp
$daysLeft = 0; // no days left!
} else {
$daysLeft = $wednesday-$today; // get the left days
$ts = $now + 84600 * $daysLeft; // now + seconds of one day * days left
}
?>
<h1>
Forecast for <?php echo date("Y-m-d", $ts) ?>
</h1>
在它的日期保持不变,星期三,这是正确的,然后迅速改变,一旦星期四开始。虽然我希望它保持不变,直到下周三
我认为你把问题复杂化了。
<?php
$today = time();
$nextWed = strtotime('next wednesday');
if(date('D', $today) === 'Wed') {
$ts = date('Y-m-d', $today);
} else {
$ts = date('Y-m-d', $nextWed);
}
echo '<h1>Forecast for '.$ts.'</h1>';
?>
发生了什么?
- 获取今天的时间戳
- 获取下周三的时间戳
- 如果今天是星期三,
$ts
=今天的日期 - 如果今天不是星期三,
$ts
=下星期三的日期 - 返回结果
编辑
<?php
$now = time();
$today = date('Y-m-d', $now);
if(date('D', $now) === 'Wed') { $nextWed = strtotime($today); }
if(date('D', $now) === 'Thu') { $nextWed = strtotime("$today - 1 days"); }
if(date('D', $now) === 'Fri') { $nextWed = strtotime("$today - 2 days"); }
if(date('D', $now) === 'Sat') { $nextWed = strtotime("$today - 3 days"); }
if(date('D', $now) === 'Sun') { $nextWed = strtotime("$today - 4 days"); }
if(date('D', $now) === 'Mon') { $nextWed = strtotime("$today - 5 days"); }
if(date('D', $now) === 'Tue') { $nextWed = strtotime("$today - 6 days"); }
$ts = date('Y-m-d', $nextWed);
echo '<h1>Forecast for '.$ts.'</h1>'
?>