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

我出去放像下面來獲取日期和兩個日期之間的天名的名單,我不知道如何得到這個請大家幫我如何使用PHP

週四2013年5月2日 週五2013年5月3日週六 2013年5月4日 日2013年5月5日週一 2013年5月6日

回答

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]

+0

非常感謝你 – 2013-05-02 09:45:45

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>'; 
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 
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>"; 
} 
?>