Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Response to: http://answers.yahoo.com/question/index?qid=20110911220601AAe0C33
- /* Timestamps are your friends. As well as the php function strtotime();
- http://php.net for more info
- */
- // Start with some variables
- $day = (60*60*24); // 60 secs * 60 mins * 24 hours
- $week = ($day * 7); // a day * 7 days
- $startDate = 'April 4, 2011'; // The date you wish to start retrieving weeks from
- $getWeekOne = false; // If you are using the first day of the year, you will want to look for week one (true), if you are using another date, set to false.
- // First you start with getting the first saturday of the year.
- $fSat = (int)strtotime($startDate) + ($day/2); // Returns the timestamp of Jan 1, 2011.
- $dayNum = (int)date('w',$fSat); // Returns the day of the week 0 (Sunday) - 6 (Saturday);
- if($dayNum !== 6) { // If the day is not a Saturday
- $fSat += $day * (6 - $dayNum); // Add the appropriate Seconds to the timestamp
- }
- // $fSat is not set to the first Saturday of the year.
- // It must now be checked to see if it is in the first ISO-8601 week number. (A week starts on monday).
- while((int)date('W', $fSat) !== 1 && $getWeekOne){
- $fSat += $week; // Add A week to the timestamp.
- }
- /* NOTE: Jan 1, 2011 is a Saturday, but because Monday is the first day of the
- week, Jan 1, 2011 is actually week 52 of 2010. So, Week 1 Saturday is the 8th of January. */
- $sats = Array();
- $cWeek = (int)date('W',$fSat);
- while( $cWeek <= 52){
- $sats[] = $cWeek . ': ' . date("D MjS, Y", $fSat);
- $fSat += $week;
- $cWeek++;
- }
- echo '<pre>';
- print_r($sats);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement