Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts

    PHp Date Dropdown

    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?

    Thanks,
    Lee.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    So you want a list of dates, every 7 days?

    PHP Code:
    $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.

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    A little easier would be to use 'next sunday' or +1 week from a value:
    PHP Code:
    <select>
    <?php
    $iFirstDate 
    strtotime('February 19, 2012');
    $iLastDate strtotime('+3 month'$iFirstDate);

    $iCurrentDate $iFirstDate;
    while (
    $iCurrentDate $iLastDate)
    {
        
    printf('<option value="%d">%s</option>' PHP_EOL$iCurrentDatedate('d F Y'$iCurrentDate));
        
    $iCurrentDate strtotime('next sunday'$iCurrentDate);
    }
    ?>
    </select>

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts
    Sorry for double post but figured it out lol

    Just set this below:
    PHP Code:
    $iFirstDate strtotime('next sunday'time()); 
    Thank you both for your help!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •