note 51934 added to function.mktime
| From: | forums at jefferyfernandez dot id dot au | Date: | Fri, 15 Apr 2005 15:33:22 +0000 |
| Subject: | note 51934 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88062@lists.php.net to get a copy of this message | ||
With response to Robert Christiaanse's Delphi related convertion to find the
day in the week of a year, here is my solution
The original purpose of this function was to return the "first day of the week"
for a timesheet application. Since PHP defaults to Monday as the start of the
week, and the need for my app to have Sunday as the start of the week, I had to
pass the day as an argument. So if you call the function like:
find_first_day_ofweek(16, 2005, 'sunday');
it would return the unix timestamp of the Sunday in week 16 of 2005.
<?php
/**
* Function to find the day of a week in a year
* @param integer $week The week number of the year
* @param integer $year The year of the week we need to calculate on
* @param string $start_of_week The start day of the week you want returned
* Monday is the default Start Day of the Week in PHP. For
* example you might want to get the date for the Sunday of wk 22
* @return integer The unix timestamp of the date is returned
*/
function find_first_day_ofweek($week, $year, $start_of_week='sunday')
{
// Get the target week of the year with reference to the starting day of
// the year
$target_week = strtotime("$week week", strtotime("1 January $year"));
// Get the date information for the day in question which
// is "n" number of weeks from the start of the year
$date_info = getdate($target_week);
// Get the day of the week (integer value)
$day_of_week = $date_info['wday'];
// Make an adjustment for the start day of the week because in PHP the
// start day of the week is Monday
switch (strtolower($start_of_week))
{
case 'sunday':
$adjusted_date = $day_of_week;
break;
case 'monday':
$adjusted_date = $day_of_week-1;
break;
case 'tuesday':
$adjusted_date = $day_of_week-2;
break;
case 'wednesday':
$adjusted_date = $day_of_week-3;
break;
case 'thursday':
$adjusted_date = $day_of_week-4;
break;
case 'friday':
$adjusted_date = $day_of_week-5;
break;
case 'saturday':
$adjusted_date = $day_of_week-6;
break;
default:
$adjusted_date = $day_of_week-1;
break;
}
// Get the first day of the weekday requested
$first_day = strtotime("-$adjusted_date day",$target_week);
//return date('l dS of F Y h:i:s A', $first_day);
return $first_day;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51934
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51934&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51934&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51934&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51934&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51934&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51934&report=yes
Search -- http://master.php.net/manage/user-notes.php