Getting the first monday of the month
I thought I should share with my fellows this function I just created to get the first monday of the month using PHP.
function get_firstmonday($month,$year) {
$num = date(”w”,mktime(0,0,0,$month,1,$year));
if($num>1)
return date(”Y-M-d H:i:s”,mktime(0,0,0,$month,1,$year)+(86400*(8-$num)));
else
return date(”Y-M-d H:i:s”,mktime(0,0,0,$month,1,$year));
}
To use this function, simply provide your month as a number ranging from 1~12 and $year ranging from 1970 ~ future:
echo get_firstmonday(5,2008);
Update: please go to the comments for the correction of this function.
Thanks to Ben Donoghue for the correction!






Ben Donoghue said,
April 21, 2008 @ 9:21 am
Hey,
Just had a nosey at your function and cool, it works great…
…except when you try something like [code]echo get_firstmonday(6,2008);[/code]
This gives 2008-06-09 00:00:00 which is the second Monday. This is because date(’w') gives the days of the week STARTING ON SUNDAY as 0. Therefore, any month starting on a Sunday will not work correctly. For myself I’ve made a small change if you’re interested:
[code]
function get_firstmonday($month,$year){
$num = date(”w”,mktime(0,0,0,$month,1,$year));
if($num==1)
return date(”Y-M-d H:i:s”,mktime(0,0,0,$month,1,$year));
elseif($num>1)
return date(”Y-M-d H:i:s”,mktime(0,0,0,$month,1,$year)+(86400*(8-$num)));
else
return date(”Y-M-d H:i:s”,mktime(0,0,0,$month,1,$year)+(86400*(1-$num)));
}
[/code]
I’ve not tried it but you may be able to use date(’N') in your original function to get Sunday as 7 instead of 0.
Cheers for it!
phpcurious said,
April 22, 2008 @ 8:08 am
@Ben,
First of all, thank you very much for visiting my site.
Yes, you are right about my function. I overlooked it in that way.
And thanks for the correction.
karlamarie said,
May 16, 2008 @ 6:26 am
How about getting the second, third and fourth Mondays?
Can you please help me?
phpcurious said,
May 16, 2008 @ 7:19 am
@karlamarie,
thanks for visiting my site.
after getting the first monday of the month, maybe, you can just add up
7 days to the first monday to get second monday of the month. and, another 7 days to the second monday to get the third monday of the month, and so on.
for example, after calling the function, change it back to unix timestamp. And then, add (7*60*60*24) to it, and then calculate it with PHP date function.
Ranjita said,
July 15, 2008 @ 9:12 am
Hi
i have a posted date and day number (sunday=0, monday=1 etc),
i want a function that calculate next immediate date depending on the day number passed to the function.
I have a function which calculate the first, last,second ,forth day of current month… but i this function wont work if i give the posted date .
$answer=strftime(”%d-%m-%Y”, get_first_day($daym, $cur_month, $cur_year,$onthe));
function get_first_day($day_number, $month, $year, $onthe)
{
$month = ($month === false) ? strftime(”%m”): $month;
$year = ($year === false) ? strftime(”%Y”): $year;
$first_day = 1 + ((7+$day_number - strftime(”%w”, mktime(0,0,0,$month, 1, $year)))%7);




if($onthe ==’1′
{
$set_value =”";
}
elseif($onthe ==’2′
{
$set_value =”7″;
}
elseif($onthe ==’3′
{
$set_value =”14″;
}
elseif($onthe ==’4′
{
$set_value =”21″;
}
elseif($onthe ==’5′
{
//if current day of week is sunday, than only we manupulate this otherwise it will take $set_value =”21″;
$set_value =”21″;
}
return mktime(0,0,0,$month, $first_day+$set_value, $year);
}