I’m trying to write a calendar function like this
function get_date($month, $year, $week, $day, $direction)
{
....
}
$week is a an integer (1, 2, 3…), $day is a day (Sun, Mon, …) or number, whichever is easier. The direction is a little confusing, because it does a different calculation.
For an example, let’s call
get_date(5, 2009, 1, 'Sun', 'forward');
It uses the default, and gets the first Sunday in May ie 2009-05-03. If we call
get_date(5, 2009, 2, 'Sun', 'backward');
, it returns the second last Sunday in May ie 2009-05-24.
Perhaps it can be made quicker…
This was VERY interesting to code.
Please note that
$directionis 1 for forward and -1 for backward to ease things up 🙂Also,
$daybegins with a value of 1 for Monday and ends at 7 for Sunday.I’ve tested it with a few examples and seems to work always, be sure to double-check it though 😉