#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Strtotime question


    Hi;

    I want the drop down to show dayes from today + 90 days.

    But I am doing something wrong. Please see what I am doing wrong.

    Thank you.
    PHP Code:
            <?php 
                $day_date 
    strtotime('now');
                while(
    $day_date strtotime('+90 days'))
                    {
                        
    ?>
                        <option value="<?php echo $day_date;?>"><?php echo date('D, jS m Y'$day_date);?></option>
           <?php     
                    $day_date 
    strtotime($day_date'+ 1 days'); 
                    } 
    ?>
    Or
    PHP Code:
    <?php 
                            $day_date 
    strtotime('now');
                            while(
    $day_date strtotime('+90 days'))
                                {
                                    
    ?>
                                    <option value="<?php echo $day_date;?>"><?php echo date('D, jS m Y'$day_date);?></option>
                       <?php     
                                   $day_date 
    date('d/m/y',$day_date);
                                
    $day_date strtotime($day_date'+ 1 days'); 
                                } 
    ?>
    Last edited by zxcvbnm; August 15th, 2013 at 11:49 PM.
  2. #2
  3. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Nevermind, found it! strtotime expects ENGLISH date format! $day_date = strtotime($day_date. '+ 1 days');
    PHP Code:
    $day_date = strtotime('now');
    while($day_date < strtotime('+90 days'))
        {
            ?>
            <option value="<?php echo $day_date;?>"><?php echo date('D, jS m Y'$day_date);?></option>
    <?php     
        $day_date 
    date('m/d/Y',$day_date);
        
    $day_date strtotime($day_date'+ 1 days'); 
        
    date('d/m/Y'$day_date);
        
        } 
    ?>
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I wonder why everybody loves juggling with those old time functions so much. We have the DateTime class since 7 years.

    Your date calculations will also give you wrong results depending on how long the loop takes, because you recalculate the end date with every iteration and check for <. If the next second begins while the loop runs, you get 91 entries, otherwise you get 90 entries.

    A correct and less cumbersome approach would be something like

    PHP Code:
    <?php

    $date 
    = new DateTime();

    // show 90 days starting today
    for ($day 1$day <= 90$day++)
    {
        echo 
    $date->format('D, jS m Y') . '<br />';
        
    $date->modify('+1 day');
    }
    Using the DateTime class, it's really simple, because you don't need to go back and forth with date() and strtotime().

    // And this misformatted PHPHTML spaghetti code is terrible -- but I guess you already knew that.
    Last edited by Jacques1; August 16th, 2013 at 02:44 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    how would you re-write it then? I can't see any other solution?

    You can do all that at the top of the page creating an array and then have that array loop echoing out the values, this way there would be "less spaghetti" code but php would be doing a bit extra work.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Do you really think PHP will choke on 90 objects?

    But even if you stick to the current loop, you can make the code a lot more readable by

    a) indenting it properly
    b) replacing the small HTML snippet with an echo or using the verbose statement syntax (endwhile, endif etc.)

    PHP Code:
    <?php 

    $date 
    = new DateTime(); 

    // show 90 days starting today 
    for ($day 1$day <= 90$day++) 

        
    $value $date->getTimestamp();
        
    $display $date->format('D, jS m Y');
        echo 
    '<option value="' escape_html($value) . '">' escape_html($display) . '</option>'
        
    $date->modify('+1 day'); 
    }
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by Jacques1
    I wonder why everybody loves juggling with those old time functions so much.
    because sometimes objects are just overkill. Sure date calculations are best performed in the DateTime class family, but date() is well suited to a 'quick to write' and 'quick to execute' timestamp to date operation.

    I don't see the need for both date and strtotime, for example:

    PHP Code:
    //
    for($i=time();$i<(time()+(90*24*60*60));$i+=(24*60*60)) {
        echo 
    date("Y-m-d",$i)."<br />\n";

    you could assign those values to variables before the loop to cut down on the number of function calls...and maybe even use static values if your app doesn't need the flexibility
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    php may not choke. but if we all did that for everypage, then PHP will have to do extra work for each USER which means a bit of a delay surely? In terms of efficiency etc

    Comments on this post

    • Northie agrees : code should be lean and mean
    • Jacques1 disagrees : Did you do a benchmark? Will you exchange the readability of your code for some nanoseconds?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Contrary to popular belief, a day does not always have 24 hours -- due to daylight saving time. So a lot of the Unix timestamp fumbling (including) this one) is bound to fail.

    Now the result not only depends on how long the loop runs. It also depends on when the code runs.

    This is hardly an acceptable solution. Personally, I also find those second calculations very annoying when going through a longer piece of code.

    Which again brings up the question: Why fumble with unreadable and error-prone low-level date functions when you can express the same thing in a robust and readable way?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,870
    Rep Power
    6351
    Or we could all just use mktime() and avoid the overhead of instantiating the DateTime object and avoid the daylight savings problem.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    Your date calculations will also give you wrong results depending on how long the loop takes, because you recalculate the end date with every iteration and check for <.
    Would you please tell me when it is going to be wrong? This shows the dates for next 1000 days and it looks right.

    Thank you.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Let's say you wanna display two days, today and tomorrow. Then your condition would be $day_date < strtotime('+1 day'), right? With your code, you'll get one, two or three days, depending on when and how long the script runs:

    If the script runs after midnight on, say, 2013-08-17 15:00, you'll get two days, because the check for "tomorrow" will be 2013-08-18 00:00 < 2013-08-18 15:00, which is true. This is the usual case.

    If the script runs on 2013-08-17 00:00:00 and takes less than a second, you'll only get one day, because the check for "tomorrow" will be 2013-08-18 00:00:00 < 2013-08-18 00:00:00, which is false.

    If the script runs on 2013-08-17 23:59:59 and takes two seconds, you'll get three days, because time('+1 day') will become 2013-08-19 00:00:01, and 2013-08-19 00:00 < 2013-08-19 00:00:01 is true.

    Now, you may hope that none of those edge cases will occur. In other words, your users shouldn't visit the site around midnight. But I wouldn't consider this to be an acceptable solution.

    There are two problems: First of all, you recalculate the end date with every iteration. This will lead to inconsistent results around midnight. Secondly, you compare the dates with <. This will lead to inconsistent results depending on the duration of the loop.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  22. #12
  23. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    Let's say you wanna display two days, today and tomorrow. Then your condition would be $day_date < strtotime('+1 day'), right? With your code, you'll get one, two or three days, depending on when and how long the script runs:

    If the script runs after midnight on, say, 2013-08-17 15:00, you'll get two days, because the check for "tomorrow" will be 2013-08-18 00:00 < 2013-08-18 15:00, which is true. This is the usual case.

    If the script runs on 2013-08-17 00:00:00 and takes less than a second, you'll only get one day, because the check for "tomorrow" will be 2013-08-18 00:00:00 < 2013-08-18 00:00:00, which is false.

    If the script runs on 2013-08-17 23:59:59 and takes two seconds, you'll get three days, because time('+1 day') will become 2013-08-19 00:00:01, and 2013-08-19 00:00 < 2013-08-19 00:00:01 is true.

    Now, you may hope that none of those edge cases will occur. In other words, your users shouldn't visit the site around midnight. But I wouldn't consider this to be an acceptable solution.

    There are two problems: First of all, you recalculate the end date with every iteration. This will lead to inconsistent results around midnight. Secondly, you compare the dates with <. This will lead to inconsistent results depending on the duration of the loop.
    PHP Code:
    <?php 
    $date 
    = new DateTime();
    for (
    $day 1$day <= 120$day++)
        {
            
    $dates_dd[strtotime($date->format('Y-m-d'))] = $date->format('D, jS M Y');
            
    $date->modify('+1 day');
        }
    echo 
    form_dropdown('available_date'$dates_dd'-','class="form-control"')
    ?>

    Comments on this post

    • Jacques1 agrees : Looks good.

IMN logo majestic logo threadwatch logo seochat tools logo