在php中以特定格式打印开始日期和结束日期的数组

时间:2018-06-11 09:17:12

标签: php arrays

以下是将给定的开始日期和结束日期分为几周的代码段:

$start_date='2018-06-11';

$end_date='2018-06-29';
$output = array();
while($start_date<=$end_date){

$friday=date ('Y-m-d', strtotime ('friday', strtotime($start_date)));

  $a['start']=$start_date;
  $a['end']=$friday;
 $output[] = $a;


$monday=date ('Y-m-d', strtotime ('next monday', strtotime($start_date)));

  $start_date=$monday;
} 

print_r($output);

它产生以下结果显示3周&#39;开始日期和结束日期:

Array ( [0] => Array ( [start] => 2018-06-11 [end] => 2018-06-15 ) [1] => Array ( [start] => 2018-06-18 [end] => 2018-06-22 ) [2] => Array ( [start] => 2018-06-25 [end] => 2018-06-29 ) )

如何以下列格式打印:

Range: 11-Jun-2018 to 15-Jun-2018
       18-Jun-2018 to 22-Jun-2018

2 个答案:

答案 0 :(得分:0)

试试这段代码

capital M用于Month name

并添加像这样的开始日期

$a['start']=date('d-M-Y',strtotime($start_date));

<?php
$start_date='2018-06-11';
$end_date='2018-06-29';
$output = array();
while($start_date<=$end_date){
    $friday=date ('d-M-Y', strtotime ('friday', strtotime($start_date)));
    $a['start']=date('d-M-Y',strtotime($start_date));
    $a['end']=$friday;
    $output[] = $a;
    $monday=date ('Y-m-d', strtotime ('next monday', strtotime($start_date)));
    $start_date=$monday;
} 
echo "<pre>";
print_r($output);
?>

答案 1 :(得分:0)

回答似乎并不好看。但我仍然推动自己。以下是您需要做的事情:

$start_date = '2018-06-11';

$end_date = '2018-06-29';
echo  "Range: ";
while ($start_date <= $end_date) {

    $friday = date('d-M-Y', strtotime('friday', strtotime($start_date)));

    echo date('d-M-Y', strtotime($start_date)) . " To " . $friday . "<br>";

    $monday = date('Y-m-d', strtotime('next monday', strtotime($start_date)));

    $start_date = $monday;
}