在PHP中的循环日期减去天数

时间:2015-08-13 10:42:12

标签: php date strtotime

这是我的代码i循环在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)));

}
?>

3 个答案:

答案 0 :(得分:1)

试试这个:

$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
        )

)

希望这会有所帮助:)

答案 1 :(得分:0)

使用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));

答案 2 :(得分:0)

如果您要在$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