note 11617 deleted from function.date by jimw

From: Date: Fri, 02 Nov 2001 00:54:10 +0000
Subject: note 11617 deleted from function.date by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-20075@lists.php.net to get a copy of this message
Here is a function for seeing how many of a certain day of the week are between 2 dates. For example, how many Saturdays are there between July 4, 2000 and October 8, 2000. The function will report the correct number. <?php // This function will determine the number of, for example, Sundays between 2 dates function determinedays($startdate,$enddate,$dayofweek) { /* Example, Count the Thursdays between Friday Feb 2, 2001 - Tuesday Feb 27, 2001 */ // Make startdate into mktime format so that we can do computations on the date // You have to convert startdate to a timestamp first $originalstart = $startdate; $startdate = strtotime($startdate); $startyear = date("Y",$startdate); $startmonth = date("n",$startdate); $startday = date("d",$startdate); $starthour = date("H",$startdate); $startminute = date("i",$startdate); $startdate = mktime($starthour,$startminute,$startsecond,$startmonth,$startday,$startyear); $nextday = mktime($starthour,$startminute,$startsecond,$startmonth,$startday+1,$startyear); // Make enddate into mktime format so that we can do computations on the date // You have to convert enddate to a timestamp first $originalend = $enddate; $enddate = strtotime($enddate); $endyear = date("Y",$enddate); $endmonth = date("n",$enddate); $endday = date("d",$enddate); $endhour = date("H",$enddate); $endminute = date("i",$enddate); $enddate = mktime($endhour,$endminute,$endsecond,$endmonth,$endday,$endyear); $dayofweek = strtolower($dayofweek); //make sure dayofweek is all lowercase $count = 0; //Number of days counted $totaldays = round(((strtotime($originalend) - strtotime($originalstart))/86400) + 0.00000001); // count through the total number of days and see how many dayofweeks or in that total for ($i=1; $i <= $totaldays; $i++) { if (strtolower(date("l",$nextday)) == $dayofweek) { $count++; } print strtolower(date("l",$nextday)) . ", "; $nextday = mktime($starthour,$startminute,$startsecond,$startmonth,$startday+$i,$startyear); } print "<BR>"; return $count; } ?>

« previous php.notes (#20075) next »