在php中重复数组日期

时间:2016-09-20 21:25:45

标签: php mysql arrays json

我需要从日期开始重复一个数组。

一个例子:

start date: 09/20/2016

$ Rounds = array ('evening', 'afternoon', 'Morning', 'Night', 'Rest');

20/09/2016 evening
21.09.2016 afternoon
22.09.2016 morning
23/09/2016 night
24/09/2016 rest
09/25/2016 evening
09.26.2016 afternoon
09.27.2016 morning
....

我有这个旧脚本,帮助我进一步改进它而不是打印示例。 20/09/2016,它会生成一个json数组,所以你可以使用插件jquery fullcalendar

$data = '03/10/2014';
$turni = array('sera','pomeriggio','Mattina','Notte','Riposo');

$n_g_mesi = array(1=>31,28,31,30,31,30,31,31,30,31,30,31);
list($gg,$mm,$aa) = explode('/', $data);
$gg = (int)$gg;
$mm = (int)$mm;
$n_g_q_mese =  $n_g_mesi[$mm];

$numero_turni = 10;

$n = count($turni);
for($i=0; $i<$numero_turni; $i++) {
    echo $data.' '.$turni[$i%$n]."<br />\n";
    $gg = $gg%$n_g_q_mese+1;

    if($gg==1) {
        $mm = $mm%12+1;
        if($mm==3 && $aa%4==0 && $n_g_q_mese==28) { // se l'anno è bisestile
            $mm = 2;
            $gg = $n_g_q_mese = 29;
        }
        else {
            $n_g_q_mese = $n_g_mesi[$mm];
            if($mm==1)
                $aa++;
        }
    }
    $data = str_pad($gg,2,'0',STR_PAD_LEFT).'/'.str_pad($mm,2,'0',STR_PAD_LEFT).'/'.$aa;
}  

谢谢

2 个答案:

答案 0 :(得分:0)

这是一个解决方案:

$aDate = '2016-09-20';
$rounds = array('Evening', 'Afternoon', 'Morning', 'Night', 'Rest');
for($i=0; $i<20; $i++) {
    $nextDate = date("Y-m-d", strtotime('+1 day', strtotime($aDate)));
    echo "\n<br>" . $nextDate . " " . $rounds[$i%count($rounds)];
    $aDate = $nextDate;
}

输出:

2016-09-21 Evening 
2016-09-22 Afternoon 
2016-09-23 Morning 
2016-09-24 Night 
2016-09-25 Rest 
2016-09-26 Evening 
2016-09-27 Afternoon 
2016-09-28 Morning 
2016-09-29 Night 
2016-09-30 Rest 
2016-10-01 Evening 
2016-10-02 Afternoon 
2016-10-03 Morning 
2016-10-04 Night 
2016-10-05 Rest 
2016-10-06 Evening 
2016-10-07 Afternoon 
2016-10-08 Morning 
2016-10-09 Night 
2016-10-10 Rest

答案 1 :(得分:0)

这是另一个解决方案,用于演示在PHP中处理日期和数组的一些替代方法:

2014-03-10 Sera<br />
2014-03-11 Pomeriggio<br />
2014-03-12 Mattina<br />
2014-03-13 Notte<br />
2014-03-14 Riposo<br />
2014-03-15 Sera<br />
2014-03-16 Pomeriggio<br />
2014-03-17 Mattina<br />
2014-03-18 Notte<br />
2014-03-19 Riposo<br />

输出:

{{1}}