note 73285 added to ref.datetime

From: Date: Fri, 16 Feb 2007 03:50:10 +0000
Subject: note 73285 added to ref.datetime
Groups: php.notes 
Request: Send a blank email to php-notes+get-123473@lists.php.net to get a copy of this message
Here is my function to count the number days, weeks, months, and year. I tried it below 1970 and it works. <?php function datecal($date,$return_value) { $date = explode("/", $date); $month_begin = $date[0]; $month_begin_date = $date[1]; $year1 = $date[2]; $month_end = date("n"); $month_end_date = date("j"); $year2 = date("Y"); $days_old = 0; $years_old = 0; $months_old = 0; if($month_begin==12) { $month = 1; $year = $year1+1; } else { $month = $month_begin+1; $year = $year1; } $begin_plus_days = cal_days_in_month(CAL_GREGORIAN, $month_begin, $year1) - $month_begin_date; $end_minus_days = cal_days_in_month(CAL_GREGORIAN, $month_end, $year2) - $month_end_date; while ($year <= $year2) { if($year == $year2) { $days_old = $days_old + cal_days_in_month(CAL_GREGORIAN, $month, $year); if($month < $month_end) { $months_old = $months_old + 1; $month = $month + 1; } elseif ($month==$month_end and $month_end_date >= $month_begin_date) { $year = $year2+1; } else { $year = $year2+1; } } else { $days_old = $days_old + cal_days_in_month(CAL_GREGORIAN, $month, $year); if ($month <= 11) { $month = $month + 1; $months_old = $months_old + 1; } else { $month = 1; $year = $year + 1; $months_old = $months_old + 1; } } } $days_old = ($days_old + $begin_plus_days) - $end_minus_days; if($return_value == "d") { return $days_old; } elseif ($return_value == "w") { return intval($days_old/7); } elseif ($return_value == "m") { return $months_old; } elseif ($return_value == "y") { return intval($months_old/12); } } echo datecal("08/13/1975","m"); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 206.49.89.226 (proxied: 10.209.10.36) ---- Manual Page -- http://www.php.net/manual/en/ref.datetime.php Edit -- https://master.php.net/note/edit/73285 Del: integrated -- https://master.php.net/note/delete/73285/integrated Del: useless -- https://master.php.net/note/delete/73285/useless Del: bad code -- https://master.php.net/note/delete/73285/bad+code Del: spam -- https://master.php.net/note/delete/73285/spam Del: non-english -- https://master.php.net/note/delete/73285/non-english Del: in docs -- https://master.php.net/note/delete/73285/in+docs Del: other reasons-- https://master.php.net/note/delete/73285 Reject -- https://master.php.net/note/reject/73285 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123473) next »