note 50209 deleted from function.strftime by derick
| From: | derick@php.net | Date: | Sat, 04 Nov 2006 10:42:46 +0000 |
| Subject: | note 50209 deleted from function.strftime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119380@lists.php.net to get a copy of this message | ||
Note Submitter: mflaig at pro-linux dot de
----
Hi there,
i had the problem that I needed all days of an week with some extra infos. given is day, month, year
(selectboxes).
so I hacked this together ...
function ...
<?php
function get_weekdates($year, $month, $day){
setlocale(LC_ALL, "C");
//echo "Year $year<br>";
//echo "Month $month<br>";
//echo "Day $day<br>";
// make unix time
$searchdate = mktime(0,0,0,$month,$day,$year);
//echo "Searchdate: $searchdate<br>";
// let´s get the day of week // on solaris <8 the first day of week is sunday, not
monday
$day_of_week = strftime("%u", $searchdate);
//echo "Debug: $day_of_week <br><br>";
$days_to_firstday = ($day_of_week - 1); // on solaris <8 this may not work
//echo "Debug: $days_to_firstday <br>";
$days_to_lastday = (7 - $day_of_week); // on solaris <8 this may not work
//echo "Debug: $days_to_lastday <br>";
$date_firstday = strtotime("-".$days_to_firstday." days", $searchdate);
//echo "Debug: $date_firstday <br>";
$date_lastday = strtotime("+".$days_to_lastday. " days", $searchdate);
//echo "Debug: $date_lastday <br>";
$d_result = ""; // array to return
// write an array of all dates of this week
for($i=0; $i<=6; $i++) {
$y = $i + 1;
$d_date = strtotime("+".$i." days", $date_firstday);
// feel free to add more values to these hashes
$result[$y]['year'] = strftime("%Y", $d_date);
$result[$y]['month'] = strftime("%m", $d_date);
$result[$y]['day'] = strftime("%d", $d_date);
$result[$y]['dayname'] = strftime("%A", $d_date);
$result[$y]['shortdayname'] = strftime("%a", $d_date);
$result[$y]['sqldate'] = strftime("%Y-%m-%d", $d_date);
}
return $result; // return the array
}
?>
so you can read all this by doing something like that:
<?php
$week = get_weekdates($year,$month,$day);
for($i = 1; $i<=7 ; $i++) {
echo 'Year: ' . $week[$i]['year'] . '<br>';
echo 'Month: ' . $week[$i]['month'] . '<br>';
echo 'Day: ' . $week[$i]['day'] . '<br>';
echo 'Longname: ' . $week[$i]['dayname'] . '<br>';
echo 'Shortname: ' . $week[$i]['shortdayname'] . '<br>';
echo 'Sqldate: ' . $week[$i]['sqldate'] . '<br>';
echo '<br>';
}
?>
I hope this helpes someone out there ...
... notes and improvements welcome (via email / pgp preferred)
Note: It does not work on Slowlaris <8 because of the %u problem (see the other posting for
further details)
for Windows and solaris you may check out the posting of vesa dot kivisto at nepton dot fi ....
this code was inspired by pb at _remove_ pedal dot dk ´s code
Have a nice day (night || whatever),
mfl