note 40960 added to function.mktime

From: Date: Wed, 24 Mar 2004 19:00:20 +0000
Subject: note 40960 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-67117@lists.php.net to get a copy of this message
I noticed that most date-difference functions here are very complex and too powerful. Here's a simple one for simple-minded folk like myself that just returns the difference (in days) between two dates: ######################### # date_diff Function ######################### # # Calculate the difference between 2 dates in YYYY-MM-DD format. # Returns the number of days difference. # function date_diff($date1, $date2) # Note that $date2 must be later than $date1 { # Get the year, month, day values of the two dates $d1 = explode("-", $date1); $d2 = explode("-", $date2); # Construct UNIX timestamps based on the original dates $date1 = mktime(0,0,0, date($d1[1]), date($d1[2]), date($d1[0])); $date2 = mktime(0,0,0, date($d2[1]), date($d2[2]), date($d2[0])); $tmp = $date1; $y = $d1[0]; $m = $d1[1]; $d = $d1[2]; $days = 0; # Increments $tmp one day at a time until it matches $date2 while($tmp != $date2) { $d++; $tmp = mktime(0,0,0, date($m), date($d), date($y)); $days++; } # while # Returns the number of increments for the dates to match return $days; } # date_diff() ?> Hope this helps solve the basic problems. ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40960 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40960&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40960&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67117) next »