note 50209 added to function.strftime
| From: | mflaig at pro-linux dot de | Date: | Mon, 21 Feb 2005 23:57:42 +0000 |
| Subject: | note 50209 added to function.strftime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85340@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.strftime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50209
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50209&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50209&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50209&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50209&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50209&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50209&report=yes
Search -- http://master.php.net/manage/user-notes.php