note 66180 added to function.strtotime

From: Date: Tue, 16 May 2006 18:09:51 +0000
Subject: note 66180 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-111398@lists.php.net to get a copy of this message
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? ---- Server IP: 216.194.113.175 Probable Submitter: 72.181.22.85 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- http://master.php.net/note/edit/66180 Del: integrated -- http://master.php.net/note/delete/66180/integrated Del: useless -- http://master.php.net/note/delete/66180/useless Del: bad code -- http://master.php.net/note/delete/66180/bad+code Del: spam -- http://master.php.net/note/delete/66180/spam Del: non-english -- http://master.php.net/note/delete/66180/non-english Del: in docs -- http://master.php.net/note/delete/66180/in+docs Del: other reasons-- http://master.php.net/note/delete/66180 Reject -- http://master.php.net/note/reject/66180 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111398) next »