SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, so the issue because quite clear once I put the following outside your while loop:
    PHP Code:
    var_dump($datestrtotime($date), $end_datestrtotime($end_date)); 
    In short, because your date strings do not contain a year, it is comparing September 2013 and January 2013 (instead of January 2014). I ultimately ended up with something like the following:

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

            date_default_timezone_set
    ('Europe/London'); 
                                                                         
            
    // Start date
            
    $date date('Y-m-d');
            
    // End date
            
    $end_date date('Y-m-d'strtotime("+120 days"));

            while (
    strtotime($date) <= strtotime($end_date)) {
            
    $outputDate date('l jS F'strtotime($date));
            echo 
    "<option value=\"$outputDate\n\">$outputDate\n</option>"

            
    $date date ('Y-m-d'strtotime("+1 days"strtotime($date)));
             
            } 

        
    ?>  

    </select>

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic, I knew it would be simple.

    Many thanks


Bookmarks

Posting Permissions

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