Hi I'm working on a site that involves alot of date references. I have already implemented some PHP scripting and includes to make the site more dynamic, but would like to do the same with the date references.

I have read about the date() funtion but I'm still a newbie to php and don't have much knowledge of the syntax yet.

I want the script to always return the first and last day (monday and sunday) of the 2nd week of May, thus the 5th month.

Just give me some hints and I'm sure I'l figure out the rest;)

All that the date() function does is pretty print a timestamp that is given to it. You will need to use the mktime() function to create a timestamp in Unix format based on your criteria, and then take that timestamp and throw it into the date() function to make it output in whatever fancy date/time format you want.

great help, thanks.

But I'm afraid I've hit another roadblock

mktime only allows Hour, Minute, second, Month, day and Year as arguments, but no Week.

I want to retrieve the first day of the 2nd week as being always Monday, and the last day being always Sunday.

My gues is that you would have to check if the days returned are the first and last of the week, and if not add # and calculate again.

in reply to my previous post:

I would like to add that this calculation would require for the specific date to be retrieved every year again.

so this year, retrieving date for the 2nd monday and sunday of May would return 7 & 13

next year it would return 5 & 11

....

considering that the dates match every x years it would be possible to calculate this, however with the leap-years throw in between the calculation would become rather complex

Is there an easier aproach to this

Here is a function that you could try, It's specifically made to retrieve the start and end of the 2nd week of May given the year passed. If you need it to retrieve from another month I think you will be able to do it yourself.

function Get2ndWeek($year)
{
$result = array();
 
// Check if the first day of may falls on a monday
$FirstDay = getdate(mktime(0, 0, 0, 5, 1, $year);
 
// compute how many days is there to the Monday after May 1st
switch($FirstDay["wday"])
{
case 0 : $DaysBeforeNext = 1; break; // Sunday
case 1 : $DaysBeforeNext = 7; break; // Monday
case 2 : $DaysBeforeNext = 6; break; // Tuesday
case 3 : $DaysBeforeNext = 5; break; // Wednesday
case 4 : $DaysBeforeNext = 4; break; // Thursday
case 5 : $DaysBeforeNext = 3; break; // Friday
case 6 : $DaysBeforeNext = 2; break; // Saturday
} 
$NextMonday = $DaysBeforeNext + 1; // 1 is added to signify that we start counting after May 1st
 
$result["Start"] = getdate(mktime(0, 0, 0, 5, $NextMonday, $year));
$result["End"] = getdate(mktime(0, 0, 0, 5, $NextMonday + 6, $year));
 
return $result;
}
This article has been dead for over six months. Start a new discussion instead.