note 101788 added to function.date-diff
| From: | kshegunov at gmail dot com | Date: | Tue, 11 Jan 2011 11:58:29 +0000 |
| Subject: | note 101788 added to function.date-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175629@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 82.177.164.100
Probable Submitter: 212.87.1.131
----
Manual Page -- http://www.php.net/manual/en/function.date-diff.php
Edit -- https://master.php.net/note/edit/101788
Del: integrated -- https://master.php.net/note/delete/101788/integrated
Del: useless -- https://master.php.net/note/delete/101788/useless
Del: bad code -- https://master.php.net/note/delete/101788/bad+code
Del: spam -- https://master.php.net/note/delete/101788/spam
Del: non-english -- https://master.php.net/note/delete/101788/non-english
Del: in docs -- https://master.php.net/note/delete/101788/in+docs
Del: other reasons-- https://master.php.net/note/delete/101788
Reject -- https://master.php.net/note/reject/101788
Search -- https://master.php.net/manage/user-notes.php