2016-04-03 45 views
0

我需要根据以前的日期生成几个日期。示例:我的日期格式为dd-mm-yyyy 开始日期从数据库获取,名称为$ startDate。根据数据库值生成日期范围

[01-01-2016 - 03-01-2016] 
[04-01-2016 - 06-01-2016] 
[07-01-2016 - 10-01-2016] 

这个差距值是从数据库中获得的。我需要使用php循环生成这种日期集。

回答

0

这样的事情,这取决于你的需求:

<?php 
$dates = [ //new php 'short array' syntax, 
//$dates = array(/* old php start */ 
'01-01-2016'=> '03-01-2016', 
'04-01-2016'=> '06-01-2016', 
'07-01-2016'=> '10-01-2016' 
//); /* old php end */ 
]; 
$f = 'd-m-Y'; //date Format 
foreach ($dates as $start=>$end){ 
    $startDate = date_create_from_format($f, $start); 
    $prevDate = date_create_from_format($f, $end); 
    $gapDate = clone($prevDate); //pay attention to this line, remove it and see what happens 
    $gapDate->add(
     new DateInterval('P1M1D') //google://ISO_8601, PHP manual 
    ); 

    echo "orig:". $startDate->format($f) . " => " . $prevDate->format($f) ."\n"; 
    echo "gap_:". $prevDate->format($f) . " => " . $gapDate->format($f) ."\n"; 
} 

//no closing tag here 
+0

可你把这段代码转换成PEO PHP。因为我在做结构化的PHP。非常感谢哟mucn –

+0

什么是peo php?你的意思是使用程序调用而不是对象调用?你知道,你可以自由地混合它们,没有什么问题 – strangeqargo