note 84434 modified in function.strtotime by danbrown

From: Date: Sun, 20 Jul 2008 03:52:09 +0000
Subject: note 84434 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142095@lists.php.net to get a copy of this message
Assuming a week starts on Monday. To calculate the week number of the current date starting from a specified date, use this function: <?php function weekcounter($start_date) { $start_date = strtotime($start_date); $today = strtotime(date('d-m-Y')); $week = 1; while($start_date < $today) { $start_date = strtotime('next Monday', $start_date); $week++; } return $week; } ?> Example: //now = 14-07-2008 echo weekcounter('01-07-2008'); //prints 3 echo weekcounter('01-06-2008'); //prints 8 instead of 7; because 1st jun is a Sunday so it is week 1 and 2nd june is week 2. Feel free to tweak. --was-- Assuming a week starts on Monday. To calculate the week number of the current date starting from a specified date, use this function: function weekcounter($start_date) { $start_date = strtotime($start_date); $today = strtotime(date('d-m-Y')); $week = 1; while($start_date < $today) { $start_date = strtotime('next Monday', $start_date); $week++; } return $week; } Example: //now = 14-07-2008 echo weekcounter('01-07-2008'); //prints 3 echo weekcounter('01-06-2008'); //prints 8 instead of 7; because 1st jun is a Sunday so it is week 1 and 2nd june is week 2. Feel free to tweak. http://php.net/manual/en/function.strtotime.php

« previous php.notes (#142095) next »