note 66180 deleted from function.strtotime by bjori
| From: | bjori@php.net | Date: | Tue, 16 May 2006 21:57:01 +0000 |
| Subject: | note 66180 deleted from function.strtotime by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-111418@lists.php.net to get a copy of this message | ||
Note Submitter:
----
What if you wanted an age in years, months and days? I took a crack at it, but the problem is that
is is not exact...I need it to be right...here is the code and an example of why it's wrong:
<?
$then = mktime(0,0,0,4,15,2005);
$time = time();
$now = mktime(5, 0, 0, date('m', $time),date('d', $time),date('Y',
$time));
$until = $now - $then;
$year = date('y', $until) - 70;
$month = date('n', $until) - 1;
$day = date('j', $until);
$hour = date('g', $until);
$minute = date('i', $until);
$second = date('s', $until);
if ($year == 1) $year_out = 'year';
else $year_out = 'years';
if ($month == 1) $month_out = 'month';
else $month_out = 'months';
if ($day == 1) $day_out = 'day';
else $day_out = 'days';
echo $year . ' ' .$year_out . ', ' . $month . ' ' . $month_out .
' and ' . $day . ' ' . $day_out . ' ';
?>
This gives :
1 year, 1 month and 1 day.
While this is correct, if you change the $then date to:
$then = mktime(0,0,0,4,16,2005);
then your output will be:
1 year, 0 months and 31 days
it should be :
1 year, 1 month and 0 days
Any thoughts on how to make it look at true months and not base it on a 31 day month?