这是我的代码,我在while循环中抛出了几个日期,我试图在循环日期之前三天创建一个startdate变量。这行不通。请帮帮我!:)
<?php
// Start date
$date = '2015-07-01';
// End date
$yesterday = '2015-08-13';
while (strtotime($date) <= strtotime($yesterday)) {
//the start date
$startdate = date(strtotime("-3 day", strtotime($date)));
//the end date
$enddate = $date;
echo $startdate . " - " . $enddate . "<br>";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
试试这个:
$startDate = new DateTime('2015-07-01');
$currentDate = new DateTime('2015-08-13');
$dates = [];
while ($currentDate >= $startDate) {
$dates[] = clone $currentDate;
$currentDate->modify('-3 days');
}
print_r($dates);
当当前日期大于或等于循环中的开始日期时,它将当前日期递减三天,克隆每个实例并创建一个向后的列表。
这个收益率:
Array
(
[0] => DateTime Object
(
[date] => 2015-08-13 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[1] => DateTime Object
(
[date] => 2015-08-10 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[2] => DateTime Object
(
[date] => 2015-08-07 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[3] => DateTime Object
(
[date] => 2015-08-04 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[4] => DateTime Object
(
[date] => 2015-08-01 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[5] => DateTime Object
(
[date] => 2015-07-29 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[6] => DateTime Object
(
[date] => 2015-07-26 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[7] => DateTime Object
(
[date] => 2015-07-23 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[8] => DateTime Object
(
[date] => 2015-07-20 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[9] => DateTime Object
(
[date] => 2015-07-17 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[10] => DateTime Object
(
[date] => 2015-07-14 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[11] => DateTime Object
(
[date] => 2015-07-11 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[12] => DateTime Object
(
[date] => 2015-07-08 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[13] => DateTime Object
(
[date] => 2015-07-05 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[14] => DateTime Object
(
[date] => 2015-07-02 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
)
希望这对你有帮助
用date()和mktime()创建一个日期:
$date = '2015-07-01';
// fill $year,$month and $day with exploded values
list($year,$month,$day) = explode("-",$date);
// add 3 Days
echo date("Y-m-d",mktime(0,0,0,$month,$day+3,$year));
// 10 Days earlier
echo date("Y-m-d",mktime(0,0,0,$month,$day-10,$year));
// 2 Month earlier
echo date("Y-m-d",mktime(0,0,0,$month-2,$day,$year));
如果您试图列出$startDate
和$endDate
之间的日期,每次间隔3天:
$startDate = new Datetime('2015-07-01');
$endDate = new Datetime('2015-08-13');
$interval = new DateInterval('P3D');
while ($startDate->format('Y-m-d') < $endDate->format('Y-m-d')) {
echo $startDate->format('Y-m-d').'<br />';
$startDate->add($interval);
}
echo $endDate->format('Y-m-d').'<br />';
如果您需要反向操作,只需使用sub()
而不是add()
,并将$startDate
和$endDate
颠倒。