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 關注
- 116 瀏覽
添加回答
舉報