Page 1 of 1

Getting all the Fridays from a month

Posted: Mon Oct 27, 2003 9:57 am
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:

Posted: Mon Oct 27, 2003 10:16 am
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'])

Posted: Mon Oct 27, 2003 10:20 am
by ruud
thanks volka - i'll give it a go! :lol:

Posted: Mon Oct 27, 2003 11:14 am
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:

Posted: Mon Oct 27, 2003 11:24 am
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 ;)