如何使用php获取两个日期之间的日期和日期列表

时间:2013-05-02 09:29:11

标签: php

<?php
$start=date('2013-05-02');
$end=date('2013-05-06');
?>

我下载了如下,我不知道如何得到这个请帮帮我

2013年4月2日星期四 2013-05-03周五 星期六2013-05-04 2013-05-05周日 星期一2013-05-06

4 个答案:

答案 0 :(得分:3)

$start    = new DateTime('2013-5-02');
$end      = new DateTime('2013-6-02');
$interval = DateInterval::createFromDateString('1 day');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt)
{
    echo $dt->format("l Y-m-d");
    echo "<br>";
}

注意[它仅支持5.3.0 php版本以上]

答案 1 :(得分:1)

根据here调整答案,您可以轻松完成此操作。

function createDateRangeArray($strDateFrom,$strDateTo)
{
    // takes two dates formatted as YYYY-MM-DD and creates an
    // inclusive array of the dates between the from and to dates.

    // could test validity of dates here but I'm already doing
    // that in the main script

    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('l Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

$start=date('2013-05-02');
$end=date('2013-05-06');

echo '<pre>'.print_r(createDateRangeArray($start, $end), 1).'</pre>';

答案 2 :(得分:0)

这对你有用

<?php
function dateRange($start, $end) {
    date_default_timezone_set('UTC');

    $diff = strtotime($end) - strtotime($start);

    $daysBetween = floor($diff/(60*60*24));

    $formattedDates = array();
    for ($i = 0; $i <= $daysBetween; $i++) {
        $tmpDate = date('Y-m-d', strtotime($start . " + $i days"));
        $formattedDates[] = date('l Y-m-d', strtotime($tmpDate));
    }    
    return $formattedDates;
}


$start='2013-05-02';
$end='2013-05-06';

$formattedDates = dateRange($start, $end);

echo join(', ', $formattedDates); 
// => Thursday 2013-05-02, Friday 2013-05-03, Saturday 2013-05-04, Sunday 2013-05-05, Monday 2013-05-06

答案 3 :(得分:0)

检查一下,

<?php
$from_date = strtotime("2013-05-02");
$to_date   = strtotime("2013-08-02");

for ($current_date = $from_date; $current_date <= $to_date; $current_date += (60 * 60 * 24)) { // looping for avvailable dates
    // use date() and $currentDateTS to format the dates in between
    $date = date("Y-m-d",$current_date);
    $day_name = getdate($current_date) ;
    $day_name = $day_name['weekday'];

    echo $date." ".$day_name."<br>";
}   
?>