note 109661 modified in dateperiod.construct by danbrown

From: Date: Fri, 10 Aug 2012 16:12:19 +0000
Subject: note 109661 modified in dateperiod.construct by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190001@lists.php.net to get a copy of this message
When you add the time 23:59:59 to the end DateTime object something like the following then the end date will be included in the period:

<?php
$date_start = new DateTime('2012-03-12');
$date_end = new DateTime('2012-03-22 23:59:59');

$interval = '+2 days';
$date_interval = DateInterval::createFromDateString($interval);

$period = new DatePeriod($date_start, $date_interval, $date_end, DatePeriod::EXCLUDE_START_DATE);

foreach($period as $dt) {
 echo $dt->format('d/m');
}
?>

OUTPUT:
14/03
16/03
18/03
20/03
22/03 --was-- When you add the time 23:59:59 to the end DateTime object something like the following then the end date will be included in the period: $date_start = new DateTime('2012-03-12'); $date_end = new DateTime('2012-03-22 23:59:59'); $interval = '+2 days'; $date_interval = DateInterval::createFromDateString($interval); $period = new DatePeriod($date_start, $date_interval, $date_end, DatePeriod::EXCLUDE_START_DATE); foreach($period as $dt) { echo $dt->format('d/m'); } OUTPUT: 14/03 16/03 18/03 20/03 22/03 http://php.net/manual/en/dateperiod.construct.php

« previous php.notes (#190001) next »