tags:

views:

205

answers:

2

Hi,

I am working on a calendar script where in i need to repeat a calendar event like "Repeat on every 1st Tuesday of every month"

In the example above. How do you get the "1st"?

for example today is June 12,2009 this would mean it's "2nd Friday of June" How do i get the "2nd"?

Thanks in advance

+11  A: 

Divide by 7 and round up:

ceil(date('j') / 7);
nickf
Heh I was trying to think of how to solve this problem. This didn't occur to me. Clean, elegant, +1.
cletus
haha yeah, it took me a minute and then I was like "oh, of course!"
nickf
Also, remember that date take an optional second parameter (timestamp), so this solution could be reused for an arbitrary date.
PatrikAkerstrand
You're awesome Nick! Good job.
drikoda
+2  A: 

This works:

<?php

$date = date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('y')));

echo date('Y-m-d', strtotime($date.'next tuesday'));

?>

The first line creates a date thats the first day of this month. The second line get the next tuesday from $date.

You can do much more see date and mktime and strtotime.

MrHus
you're doing the reverse of what the OP is asking. Given a date, tell me how many Fridays (or whatever) there have been before this in this month, not what is the date of the nth Friday?
nickf
+1 i needed this =)
drikoda