2 回答
TA贡献1817条经验 获得超14个赞
怎么样:
// We start at your given start date
$startDate = new DateTime("2020-02-01");
// We define a date when to stop additional list items
$endDate = new DateTime("2020-12-31");
// We define the current time
$now = new DateTime();
// Modify date on the fly, by given time frame (readability)
// Ensures to display dates only between start+14day && end
while ($startDate->modify("+14 days") < $now && $startDate < $endDate) {
echo '<li>' . $startDate->format("Y-m-d") . '</li>';
}
TA贡献1813条经验 获得超2个赞
我已经测试了这个,它的工作原理:
<?php
// Set the default timezone.
date_default_timezone_set('America/Edmonton');
$startDate = new DateTime('2020-02-01 00:00:00');
$startDate->modify('+14 days');
$endDate = new DateTime('2020-12-31 00:00:00');
$dateRange = new DatePeriod($startDate, new DateInterval('P14D'), $endDate);
?>
<ul>
<?php foreach($dateRange as $date) { ?>
<li>
<?php echo $date->format('Y-m-d'); ?>
</li>
<?php } ?>
</ul>
我已经添加了更改时间戳的内容(将时间戳添加2周)。由于每 2 周返回一次时间戳,因此我删除了 if 语句。您可以在此处了解更多信息。$endDate->modify('+14 days');$dateRangedate->modify()
- 2 回答
- 0 关注
- 97 浏览
添加回答
举报