note 79480 added to function.date

From: Date: Thu, 29 Nov 2007 12:30:44 +0000
Subject: note 79480 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-132661@lists.php.net to get a copy of this message
Since there aint so many datediff function which keeps in mind how many days there are in a month before counting X seconds into a full month. This one will do just that! You can give about any textual english datetime notations into the function as it will be parsed by strtotime(); If strtotime(); would fail this function will return false. Expample: <?php print_R(datediff("1-02-2008 00:00:00", "1-03-2008 00:00:00")); ?> Result: Array ( [year] => 0 [quarter] => 0 [month] => 1 [week] => 4 [day] => 28 [hour] => 672 [minute] => 40320 [second] => 2419200 ) <?php /** * * Calculates difference between 2 dates * Result is how many FULL years or quarters or months or week or days or hours or minutes or seconds * * Please do give credits when you use this, I'd appreciate that. * Please do include this header in your code aswell. * @param string $startdate string of text that consists about any English textual datetime description * @param string $enddate string of text that consists about any English textual datetime description * @return array $diff * @author Ralph 'Phoenix_the_II' de Boom * @copyright 2007 * @version 1.0 * */ function datediff($startdate, $enddate) { if (($startdate = strtotime($startdate)) === false) { return false; } if (($enddate = strtotime($enddate)) === false) { return false; } $startdate = strftime("%Y-%m-%d %H:%M:%S", $startdate); $enddate = strftime("%Y-%m-%d %H:%M:%S", $enddate); if (preg_match("/^([\d]{4})-([\d]{2})-([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})$/", $startdate, $startdate)) { if (preg_match("/^([\d]{4})-([\d]{2})-([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})$/", $enddate, $enddate)) { $date_start = mktime($startdate[4], $startdate[5], $startdate[6], $startdate[2], $startdate[3], $startdate[1]); $date_end = mktime($enddate[4], $enddate[5], $enddate[6], $enddate[2], $enddate[3], $enddate[1]); if ($date_start > $date_end) { $start = $date_end; $end = $date_start; $__startdate = $startdate; $__enddate = $enddate; $startdate = $__enddate; $enddate = $__startdate; $negative = true; } else { $end = $date_end; $start = $date_start; } $year_diff = date('y', $end) - date('y', $start); $day_diff = date('z', $end) - date('z', $start) + (((int)date('L', $start)) ? ($year_diff * 366) : ($year_diff * 365)); $days = $day_diff; $month_diff = 0; while (mktime(0, 0, 0, $startdate[2]+$i, $startdate[3], $startdate[1]) <= $end) { $daysinmonth = date("t", mktime(0, 0, 0, $startdate[2]+$i, $startdate[3], $startdate[1])); if ($days - $daysinmonth >= 0) { $days = $days - $daysinmonth; $month_diff++; } $i++; } $quarter_diff = floor($month_diff / 3); $week_diff = floor($day_diff / 7); $hour_diff = date('H', $end) - date('H', $start) + ($day_diff * 24); $min_diff = date('i', $end) - date('i', $start) + ($hour_diff * 60); $sec_diff = date('s', $end) - date('s', $start) + ($min_diff * 60); $year_diff = floor($month_diff / 12); if ($negative) { $year_diff = $year_diff - ($year_diff * 2); $quarter_diff = $quarter_diff - ($quarter_diff * 2); $month_diff = $month_diff - ($month_diff * 2); $week_diff = $week_diff - ($week_diff * 2); $day_diff = $day_diff - ($day_diff * 2); $hour_diff = $hour_diff - ($hour_diff * 2); $min_diff = $min_diff - ($min_diff * 2); $sec_diff = $sec_diff - ($sec_diff * 2); } return array( 'year' => $year_diff, 'quarter' => $quarter_diff, 'month' => $month_diff, 'week' => $week_diff, 'day' => $day_diff, 'hour' => $hour_diff, 'minute' => $min_diff, 'second' => $sec_diff); } else { return false; } } else { return false; } } ?> ---- Server IP: 83.137.20.135 Probable Submitter: 81.4.97.215 (proxied: 62.41.178.65) ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/79480 Del: integrated -- https://master.php.net/note/delete/79480/integrated Del: useless -- https://master.php.net/note/delete/79480/useless Del: bad code -- https://master.php.net/note/delete/79480/bad+code Del: spam -- https://master.php.net/note/delete/79480/spam Del: non-english -- https://master.php.net/note/delete/79480/non-english Del: in docs -- https://master.php.net/note/delete/79480/in+docs Del: other reasons-- https://master.php.net/note/delete/79480 Reject -- https://master.php.net/note/reject/79480 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132661) next »