note 101788 deleted from function.date-diff by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 Jan 2011 15:41:35 +0000 |
| Subject: | note 101788 deleted from function.date-diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175642@lists.php.net to get a copy of this message | ||
Note Submitter: kshegunov at gmail dot com
----
A little correction on the function below (sorry it just slipped my mind before):
It doesn't regard the years when calculating the days so solution would be to replace
<?php
// Calculate the days
$measure = ($months == 1) ? 'month' : 'months';
$days = $endDate - strtotime('+' . $months . ' ' . $measure,
$startDate);
$days = date('z', $days);
?>
With the following:
<?php
// Calculate the days
$offsets = array();
if ($years > 0)
$offsets[] = $years . (($years == 1) ? ' year' : ' years');
if ($months > 0)
$offsets[] = $months . (($months == 1) ? ' month' : ' months');
$offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) :
'now';
$days = $endDate - strtotime($offsets, $startDate);
$days = date('z', $days);
?>