|
|
|
Find the first weekday of a month
4
This function will let you find the Sunday, Monday, Tuesday, etc of a month and return to you that date.
It is a modification of a script I found online at this site: http://filchiprogrammer.wordpress.com/2008/02/27/getting-the-first-monday-of-the-month/.
The function takes three arguments, the month, year and the weekday you are looking for. The weekday is represented by an integer, 0 for Sunday, 1 for Monday and so on.
It is a modification of a script I found online at this site: http://filchiprogrammer.wordpress.com/2008/02/27/getting-the-first-monday-of-the-month/.
The function takes three arguments, the month, year and the weekday you are looking for. The weekday is represented by an integer, 0 for Sunday, 1 for Monday and so on.
/**
* This function calculates the first [WEEKDAY] of a month.
* The day to find is passed as an integer to the function.
*
* To use: Pass the month, year and day (as an integer 0-6) to the function.
*
* @param int $month
* @param int $year
* @param int $day [0 = sunday, 1 = monday, 2 = tuesday, 3 = wednesday, 4 = thursday, 5 = friday, 6 = saturday]
* @return date
*/
function getFirstDay($month,$year,$day){
$num = date("w",mktime(0,0,0,$month,1,$year));
if($num==$day) {
return date("Y-m-d H:i:s",mktime(0,0,0,$month,1,$year));
}
elseif($num>$day) {
return date("Y-m-d H:i:s",mktime(0,0,0,$month,1,$year)+(86400*((7+$day)-$num)));
}
else {
return date("Y-m-d H:i:s",mktime(0,0,0,$month,1,$year)+(86400*($day-$num)));
}
}
// Example of use:
$sunday = getFirstDay('1','2009',0); // Sunday
$monday = getFirstDay('1','2009',1); // Monday
$tuesday = getFirstDay('1','2009',2); // Tuesday
$wednesday = getFirstDay('1','2009',3); // Wednesday
$thursday = getFirstDay('1','2009',4); // Thursday
$friday = getFirstDay('1','2009',5); // Friday
$saturday = getFirstDay('1','2009',6); // Saturday
echo "First Sunday: " . $sunday . "<br />";
echo "First Monday: " . $monday . "<br />";
echo "First Tuesday: " . $tuesday . "<br />";
echo "First Wednesday: " . $wednesday . "<br />";
echo "First Thursday: " . $thursday . "<br />";
echo "First Friday: " . $friday . "<br />";
echo "First Saturday: " . $saturday . "<br /><br />";




There are currently no comments for this snippet.