Question

Y at-il fonction de diviser un début et une date de fin dans chuncks de jours $interval (ou mois)? Par exemple:

$interval = new DateInterval('P10D');
$start    = new DateTime('2012-01-10');
$end      = new DateTime('2012-02-16');

$chunks = splitOnInterval($start, $end, $interval);

// Now chunks should contain
//$chunks[0] = '2012-01-10'
//$chunks[1] = '2012-01-20'
//$chunks[2] = '2012-01-30'
//$chunks[3] = '2012-02-09'
//$chunks[3] = '2012-02-16'

Je pense que DatePeriod aide peut, mais je ne trouve aucun moyen sur la façon dont je peux l'utiliser.

Était-ce utile?

La solution

Vérifier cet article sur comment itérer jours de calendrier plus valides .

En php est quelque chose comme,

$start = strtotime('2012-01-10');
$end1 = strtotime('2012-02-16');
$interval   = 10*24*60*60; // 10 days equivalent seconds.
$chunks = array();
for($time=$start; $time<=$end1; $time+=$interval){
    $chunks[] = date('Y-m-d', $time);
}

Autres conseils

Voici un exemple pour itérer sur plusieurs jours, par rapport au mois travaille en conséquence avec d'autres intervalle

<?php

$begin = new DateTime( '2012-11-01' );
$end = new DateTime( '2012-11-11' );
$end = $end->modify( '+1 day' );

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
echo $date->format("Y-m-d") . "<br>";
}
?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top