note 83431 added to function.date

From: Date: Mon, 26 May 2008 06:27:18 +0000
Subject: note 83431 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-139779@lists.php.net to get a copy of this message
The date calculations designed to find the number of days between 2 dates do not fully account for the start and end date. This will: <?php function numDays($date_1,$date_2) { $start_day = strtotime(date('m/d/Y 00:00:00',strtotime($date_1))); $end_day = strtotime(date('m/d/Y 23:59:59',strtotime($date_2))); $difference = abs($end_day - $start_day); //Shouldn't need to ceil() the return, but just in case microsecond fluctuations must be accounted for return ceil($difference/(86400)); } ?> To calculate the number of months between 2 dates, this should help: <?php function numMonths($date_1, $date_2) { $date_one = strtotime($date_1); $date_two = strtotime($date_2); $smallest_date = min($date_one,$date_two); $smallest_year = date('Y',$smallest_date); $smallest_month = date('m',$smallest_date); $largest_date = max($date_one,$date_two); $largest_year = date('Y',$largest_date); $largest_month = date('m',$largest_date); $years_between = $largest_year - $smallest_year - 1; $months_from_smallest = 13 - $smallest_month; if ($largest_year == $smallest_year) { return $largest_month - $smallest_month + 1; } else { return $years_between*12 + $months_from_smallest + $largest_month; } } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 70.134.62.161 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/83431 Del: integrated -- https://master.php.net/note/delete/83431/integrated Del: useless -- https://master.php.net/note/delete/83431/useless Del: bad code -- https://master.php.net/note/delete/83431/bad+code Del: spam -- https://master.php.net/note/delete/83431/spam Del: non-english -- https://master.php.net/note/delete/83431/non-english Del: in docs -- https://master.php.net/note/delete/83431/in+docs Del: other reasons-- https://master.php.net/note/delete/83431 Reject -- https://master.php.net/note/reject/83431 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139779) next »