Advertisement
gitlez

YA: Finding the Saturdays of the Year

Sep 13th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. <?php
  2. // Response to: http://answers.yahoo.com/question/index?qid=20110911220601AAe0C33
  3.  
  4.  
  5. /*        Timestamps are your friends. As well as the php function strtotime();
  6.         http://php.net for more info
  7. */
  8.  
  9. // Start with some variables
  10. $day = (60*60*24); // 60 secs * 60 mins * 24 hours
  11. $week = ($day * 7); // a day * 7 days
  12.  
  13. $startDate = 'April 4, 2011'; // The date you wish to start retrieving weeks from
  14. $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.
  15.  
  16.  
  17. // First you start with getting the first saturday of the year.
  18. $fSat = (int)strtotime($startDate) + ($day/2); // Returns the timestamp of Jan 1, 2011.
  19. $dayNum = (int)date('w',$fSat); // Returns the day of the week 0 (Sunday) - 6 (Saturday);
  20.  
  21. if($dayNum !== 6) { // If the day is not a Saturday
  22.     $fSat += $day * (6 - $dayNum); // Add the appropriate Seconds to the timestamp
  23. }
  24. // $fSat is not set to the first Saturday of the year.
  25. // It must now be checked to see if it is in the first ISO-8601 week number. (A week starts on monday).
  26.  
  27. while((int)date('W', $fSat) !== 1 && $getWeekOne){
  28.     $fSat += $week; // Add A week to the timestamp.
  29. }
  30.  
  31. /* NOTE: Jan 1, 2011 is a Saturday, but because Monday is the first day of the
  32. week, Jan 1, 2011 is actually week 52 of 2010. So, Week 1 Saturday is the 8th of January. */
  33.  
  34. $sats = Array();
  35. $cWeek = (int)date('W',$fSat);
  36. while( $cWeek <= 52){
  37.     $sats[] = $cWeek . ': ' . date("D MjS, Y", $fSat);
  38.     $fSat += $week;
  39.     $cWeek++;
  40. }
  41.  
  42. echo '<pre>';
  43. print_r($sats);
  44.  
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement