Working with Dates is always a problem. PHP just has a limited ability to work properly with dates, some things are not so easy too find. The PEAR Library offers a good amound of possibilities to work with dates, but the price is a massive loss of speed.
After searching a while I found 3 useful functions to work with weeks. Which I adapted to my needs.
Everything should be clear otherwise you may ask in the comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/** * adapted from http://boonedocks.net/mike/archives/137-Creating-a-Date-Range-Array-with-PHP.html * input two sql date strings or timestamps and retrieve * an array of dates between the given dates, end date is not included * @param string $sStart * @param string $sEnd * @return array */ public static function dateRangeToArray($sStart, $sEnd){ $mpRange = array(); if(is_string($sStart) === true){ $sStart = strtotime($sStart); } if(is_string($sEnd) === true){ $sEnd = strtotime($sEnd); } if($sStart > $sEnd){ return self::dateRageToArray( $sEnd, $sStart ); } do{ $mpRange[] = date('Y-m-d', $sStart); $sStart = strtotime('+1 day', $sStart); }while($sStart <= $sEnd); return $mpRange; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * input a week and a year and retrieve an array * of dates for days in that week * @param integer $iWeek * @param integer $iYear * @return array */ public static function weekToDateArray($iWeek, $iYear){ list($timestamp_for_monday, $timestamp_for_sunday) = self::weekToTimestamps($iWeek, $iYear); return self::dateRangeToArray( $timestamp_for_monday, $timestamp_for_sunday ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * input a week and a year and retrieve an array of * timestamps for the first day (monday) and the last day (sunday) * @param integer $iWeek * @param integer $iYear * @return array */ public static function weekToTimestamps($iWeek, $iYear){ $timestamp_for_monday = strtotime($iYear."W".self::leadZero($iWeek). ' +2 hours'); $timestamp_for_sunday = strtotime('+6 days', $timestamp_for_monday); return array($timestamp_for_monday, $timestamp_for_sunday); } |
cheers