Getting all the Fridays from a month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ruud
Forum Newbie
Posts: 16
Joined: Mon Oct 27, 2003 9:57 am

Getting all the Fridays from a month

Post by ruud »

Hi

I'm a bit of a newbie to PHP so you'll have to be very simple in your explanations .

I'm tryng to get a form to display all Fridays in a month in date format in a drop down list. I'm passing the month and the year as a variable from another form, the variables are $month and $year.

I've looked at the get date function but can't work out how to get it to produce a list of all fridays in a month.

Then i would like to add either 7 days, 14 days or 21 days to this date to get an end date (it's for hiring a vehicle) which I can then check against a DB to see if any of those dates are booked.

Thanks in advance for any help with this.

Cheers
:lol:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe this little script helps you

Code: Select all

<table border="1">
<?php
$year = 2003;
for ($month=1; $month!=13; $month++)
{
	echo '<tr>';
	$ts = mktime( 12, 0, 0, $month, 1, $year);
	$date = getdate($ts);
	echo '<td><pre>'; print_r($date); echo '</pre></td>';
	
	$toFriday = ($date['wday'] > 5) ? 12-$date['wday'] : 5-$date['wday'];
	$ts = mktime ( 12, 0, 0, $month, 1+$toFriday, $year);
	$date = getdate ($ts);
	echo '<td><pre>'; print_r($date); echo '</pre></td>';
	echo '</tr>';
}
?></table>
$date['wday'] holds the day of week, friday is five. Then it's either 5-$date['wday'] days from now to friday or now->sunday->friday days (12-$date['wday'])
ruud
Forum Newbie
Posts: 16
Joined: Mon Oct 27, 2003 9:57 am

Post by ruud »

thanks volka - i'll give it a go! :lol:
ruud
Forum Newbie
Posts: 16
Joined: Mon Oct 27, 2003 9:57 am

Post by ruud »

thanks volka - it's displaying the first friday of every month. what i would like it to do is to loop through giving every friday for a particular month that is already set.

i'll be passing the month and the year to the page and want it to show all the fridays of that month.

thanks for all your help so far :lol:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

but that's easy.
$ts = mktime ( 12, 0, 0, $month, 1+$toFriday, $year);
simply add 7 to $toFriday as long as you stay within the same month and you got it ;)
Post Reply