note 65940 deleted from function.date by derick

From: Date: Sun, 11 Jun 2006 11:47:24 +0000
Subject: note 65940 deleted from function.date by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-114271@lists.php.net to get a copy of this message
Note Submitter: technical-info at sensson dot net ---- For those interested. Pretty quick and dirty solution. <?php /* * array week2date ( integer weeknumber, integer year ) * returns an array with the days in a particular week */ function week2date($weeknumber, $year) { // start at week 1 $week = 1; // start searching from day 1 $day = 1; // count the number of loops $loop = 1; // count the number of days found (used to create a readable array) $daycount = 1; // loop until a certain weeknumber is found while($week <= $weeknumber) { // increase $day with 7 - a week lasts 7 days $day = $day + 7; // calculate the week number $week = date('W', mktime(0, 0, 0, 1, $day, $year)); // if $loop is 1, this is the first day and loop and should not return 52 // prevent from stopping if $week returns 52 if($loop == 1 && $week == 52) { $week = 1; } // the day is in the specified week if($week == $weeknumber) { // since we found the week, put the initial day into an array $array_day = date('d', mktime(0, 0, 0, 1, $day, $year)); $array_month = date('m', mktime(0, 0, 0, 1, $day, $year)); $array_year = date('Y', mktime(0, 0, 0, 1, $day, $year)); $array[$array_year][$array_month][$array_day] = true; // increase daycount for the readable array $daycount++; // set new variables to find other days within a week $day_inc = $day; // used to increase $day_dec = $day; // used to decrease $week_inc = $week; // used to increase $week_dec = $week; // used to decrease // find other days in that week, increase by 1 first while($week_inc == $weeknumber) { // increase day by 1 as the $day_inc has already been found $day_inc = $day_inc + 1; // select the weeknumber $week_inc = date('W', mktime(0, 0, 0, 1, $day_inc, $year)); // it seems that the loop still counts 1 day off, so check again to be sure if($week_inc == $weeknumber) { // week found, put data into the array $array_day = date('d', mktime(0, 0, 0, 1, $day_inc, $year)); $array_month = date('m', mktime(0, 0, 0, 1, $day_inc, $year)); $array_year = date('Y', mktime(0, 0, 0, 1, $day_inc, $year)); $array[$array_year][$array_month][$array_day] = true; // increase daycount again $daycount++; } } // find other days by decreasing while($week_dec == $weeknumber) { // decrease days $day_dec = $day_dec - 1; // find week number $week_dec = date('W', mktime(0, 0, 0, 1, $day_dec, $year)); // extra check if($week_dec == $weeknumber) { // week found, put data into the array $array_day = date('d', mktime(0, 0, 0, 1, $day_dec, $year)); $array_month = date('m', mktime(0, 0, 0, 1, $day_dec, $year)); $array_year = date('Y', mktime(0, 0, 0, 1, $day_dec, $year)); $array[$array_year][$array_month][$array_day] = true; // increase daycount $daycount++; } } // everything was found, break the loop break; } // increase loop count $loop++; } $i = 0; // build sorting ksort($array); foreach($array as $year => $month_array) { ksort($month_array); foreach($month_array as $month => $day_array) { ksort($day_array); foreach($day_array as $day => $day_boolean) { $new_array[$i]['day'] = $day; $new_array[$i]['month'] = $month; $new_array[$i]['year'] = $year; $i++; } } } $array = $new_array; // return the results return $array; } ?>

« previous php.notes (#114271) next »