View Full Version : PHp Date Dropdown

Lee Stevens
01-31-2012, 09:22 PM
Hi i'm looking for some help,

I've searched Google but non fit what i need:

I need a drop down with dates in it format:

05 February 2012
12 February 2012

and so on, so the week starts on the Sunday but i need it to generate for a given set amount of months.

Could anyone help with this?


01-31-2012, 09:58 PM
So you want a list of dates, every 7 days?

$cur = strtotime('05 Feb 2012 12:00:00'); // Whenever you want the first date, and noon so you don't have issues like DST or seconds changing
$increment = 60 * 60 * 24 * 7; // Seconds in a week

while ($cur<= strtotime('05 Feb 2013 12:00:00')) {
echo date('d F y', $cur);
$cur += $increment;

Basically set a start time in seconds since Unix Epoch, figure out how much you want to increment it, then loop until you don't want to echo anymore.

Lee Stevens
01-31-2012, 10:04 PM
Thank you, yeah need it printed each week from the Sunday. for say 3 months.

How would it start from the following week if that date has gone?

01-31-2012, 10:26 PM
A little easier would be to use 'next sunday' or +1 week from a value:

$iFirstDate = strtotime('February 19, 2012');
$iLastDate = strtotime('+3 month', $iFirstDate);

$iCurrentDate = $iFirstDate;
while ($iCurrentDate < $iLastDate)
printf('<option value="%d">%s</option>' . PHP_EOL, $iCurrentDate, date('d F Y', $iCurrentDate));
$iCurrentDate = strtotime('next sunday', $iCurrentDate);

Lee Stevens
01-31-2012, 10:37 PM
Thank you Keleth and Fou-Lu. They both work the way i would like them to apart from the list must always show from the follow Sunday so next would be 05 Feb, but once 05 Feb has passed to display the next Sunday 12th.

Do you understand so it should always be three month ahead no matter what the start date was.

Thank you,

Lee Stevens
01-31-2012, 10:48 PM
Sorry for double post but figured it out lol

Just set this below:

$iFirstDate = strtotime('next sunday', time());

Thank you both for your help! :D

01-31-2012, 11:14 PM
The only real question is if it is sunday, which do you want to show? Today or next sunday?
If you want it to show today when it is sunday, use 'this sunday' (if its not sunday it will show the next). On the other hand, if it is sunday and you want to show next sunday not today, use 'next sunday'. Time() is also optional and can be omitted if you like.

Lee Stevens
01-31-2012, 11:22 PM
It's for a Booking Script so if they was to book on the actual Sunday it is then they'll have to by phone :P.