note 101990 added to datetime.diff
| From: | schindhelm at gmail dot com | Date: | Fri, 21 Jan 2011 20:48:07 +0000 |
| Subject: | note 101990 added to datetime.diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175979@lists.php.net to get a copy of this message | ||
I found that DateTime::diff isn't as accurate as I thought. I calculated the age gap between
now and a birthdate from before 1970 (unix epoch). Here's what I got:
Given today is January 21st, 2011:
<?php
date_default_timezone_set('Europe/Berlin');
// birthdate format is YYYY-MM-DD
$birth = new DateTime('1966-01-21');
$today = new DateTime();
$diff = $birth->diff($today);
echo $diff->format('%y'); // will output 45 wrongly
$birth = new DateTime('1966-01-23');
$today = new DateTime();
$diff = $birth->diff($today);
echo $diff->format('%y'); // will output 45 wrongly
$birth = new DateTime('1966-01-24'); // three days difference!
$today = new DateTime();
$diff = $birth->diff($today);
echo $diff->format('%y'); // will output 44 - correct
?>
When calculating with the date() function it was more accurate (didn't use seconds/hours for
comparison).
Note that 3 days may be a lot if you want to create invoices and have to check against a given age
to determine if the customer is chargable for taxes and so on.
If someone also found this behaviour I'd like to hear about it - give me a quick mail at
schindhelm (at) gmail (dot) com.
Thanks.
----
Server IP: 69.147.83.197
Probable Submitter: 94.217.26.85
----
Manual Page -- http://www.php.net/manual/en/datetime.diff.php
Edit -- https://master.php.net/note/edit/101990
Del: integrated -- https://master.php.net/note/delete/101990/integrated
Del: useless -- https://master.php.net/note/delete/101990/useless
Del: bad code -- https://master.php.net/note/delete/101990/bad+code
Del: spam -- https://master.php.net/note/delete/101990/spam
Del: non-english -- https://master.php.net/note/delete/101990/non-english
Del: in docs -- https://master.php.net/note/delete/101990/in+docs
Del: other reasons-- https://master.php.net/note/delete/101990
Reject -- https://master.php.net/note/reject/101990
Search -- https://master.php.net/manage/user-notes.php