www.webdeveloper.com
Results 1 to 2 of 2

Thread: PHP Stops Producing Dates After December 31st

  1. #1
    Join Date
    Oct 2012
    Posts
    16

    PHP Stops Producing Dates After December 31st

    Hi guys,

    I've created a booking form on my website for people to book appointments from a list of dates. I have it set to produce 120 days from the current day. It's been working fine all year but since the 120th day has hit December 31st/Jan 1st it's no longer producing the list. I can only assume this is due to it trying to look further than December in the same year - which there obviously isn't any more days in this year... So, how do I get it to start a new year, from Jan 1st..?

    Any help would be great.

    PHP Code:
    <option selected disabled value="select-date">Select a date</option>
                                                       
        <?php 

            date_default_timezone_set
    ('Europe/London');
                                                                        
            
    // Start date
            
    $date date('l jS F');
            
    // End date
            
    $end_date date('l jS F'strtotime("+120 days"));

            while (
    strtotime($date) <= strtotime($end_date)) {

            echo 
    "<option value=\"$date\n\">$date\n</option>";

            
    $date date ('l jS F'strtotime("+1 days"strtotime($date)));
            
            }

        
    ?> 

    </select>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    I think it's because you don't include the year in the date strings, so when you convert them via strtotime(), you are getting the date for the current year.

    My suggestion would to set $date and $end_date as UNIX timestamp integers, then use date() to convert them only when actually outputting them.
    PHP Code:
    <option selected disabled value="select-date">Select a date</option>
    <?php
    date_default_timezone_set
    ('Europe/London');
    // Start date
    $date time();
    //date('l jS F');
    // End date
    $end_date strtotime('+120 days'$date);
    while (
    $date <= $end_date)
    {
        echo 
    "<option value='".date('l jS F'$date)."'>".date('l jS F'$date)."</option>\n";
        
    $date strtotime("+1 days"$date);
    }
    ?>
    </select>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles