note 51934 deleted from function.mktime by derick
| From: | derick@php.net | Date: | Sun, 24 Sep 2006 12:00:38 +0000 |
| Subject: | note 51934 deleted from function.mktime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117514@lists.php.net to get a copy of this message | ||
Note Submitter: forums at jefferyfernandez dot id dot au
----
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;
}
?>