How to find the number of DateIntervals between two dates in PHP
-
24-06-2021 - |
Pregunta
I am trying to determine the number of date intervals between two dates. The interval could be a number of months, days, hours or whatever the user has chosen.
At the moment I have a function using recursion, however, this will break if there have been more than 100 intervals between the two points, due to the recursive nesting limit.
private static function findCurrentInterval($initialStartTimestamp, $intervalStrtotimeFormat, $now = null, $period = 1)
{
if (is_null($now)){
$now = time();
}
$endOfCurrentInterval = strtotime($intervalStrtotimeFormat, $initialStartTimestamp);
if ($now > $initialStartTimestamp && $now < $endOfCurrentInterval){
return new self($period);
}else{
# increment the period ID tracking variable
$period++;
return self::findCurrentInterval($endOfCurrentInterval, $intervalStrtotimeFormat, $now, $period);
}
}
All I need out of it is an integer value that represents which period of time intervals we are in currently, i.e. if it is set to a 7 day interval, and 20 days have passed between the start point and now, it would return '3', because we'd be into the 3rd period.
Is there a way of doing this without recursion, perhaps using DateInterval?
N.B. this would be really straightforward if it was limited to days or hours - but it needs to support months, which can have variable lengths.
Solución 2
This seems to work:
function findCurrentInterval($number, $type, $start, $end){
$dt1 = DateTime::createFromFormat('Y-m-d H:i:s', $start);
$dt2 = DateTime::createFromFormat('Y-m-d H:i:s', $end);
$diff = $dt1->diff($dt2);
$numDays = (int) $diff->format('%a');
$numMonths = (int) $diff->format('%m');
$numYears = (int) $diff->format('%y');
$numHours = (int) $diff->format('%h');
if ($numYears > 0){
$numMonths += ($numYears * 12);
}
$numHours += $numDays * 24;
switch ($type){
case 'hours':
return floor($numHours / $number);
break;
case 'days':
return floor($numDays / $number);
break;
case 'months':
return floor($numMonths / $number);
break;
default:
throw new Exception('Interval type '.$type.' not implemented.');
}
}
Examples:
echo findCurrentInterval(3, 'hours', '2012-01-01 00:00:00', '2012-08-01 00:00:00');
echo findCurrentInterval(7, 'days', '2012-01-01 00:00:00', '2012-08-01 00:00:00');
echo findCurrentInterval(1, 'months', '2012-01-01 00:00:00', '2012-08-01 00:00:00');
Outputs:
1704
30
7
And:
echo findCurrentInterval(3, 'hours', '2011-01-01 00:00:00', '2012-08-01 00:00:00');
echo findCurrentInterval(7, 'days', '2011-01-01 00:00:00', '2012-08-01 00:00:00');
echo findCurrentInterval(1, 'months', '2011-01-01 00:00:00', '2012-08-01 00:00:00');
Outputs:
4624
82
19
Otros consejos
This is very help full to you
Try this
// Day bases
$start_date = '20-07-2012';
$end_date = '22-07-2012';
$start = strtotime($start_date);
$end = strtotime($end_date);
$interval = 2;
$out='';
$int = 24*60*60*$interval;
for($i= $start;$i<= $end; $i += $int ){
echo date('d-m-Y',$i).'<br />';
}
//Month bases
$start_date = '20-07-2012';
$end_date = '22-12-2012';
$start = strtotime($start_date);
$end = strtotime($end_date);
$interval = 2;
$int = 30*24*60*60*$interval;
for($i= $start;$i<= $end; $i += $int ){
echo date('d-m-Y',$i).'<br />';
}
//Hour bases
$start_date = '20-07-2012';
$end_date = '22-12-2012';
$start = strtotime($start_date);
$end = strtotime($end_date);
$interval = 2;
$int = 60*60*$interval;
for($i= $start;$i<= $end; $i += $int ){
echo date('d-m-Y H:i:s',$i).'<br />';
}