#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    72
    Rep Power
    6

    Strtotime not working


    I have some code to add an interval of minutes to a start date and display the result. The code is as follows:

    PHP Code:
    $interval 9;
    $xtime "0846";
        for (
    $j=0;$j<20;$j++)
            {
    echo (
    $xtime."   ");
    $xtime date("Hi",strtotime($xtime $interval));
    echo (
    $xtime."<br>");
            } 
    With PHP 5.0.1 this works fine. However, when I try to run it on a Windows 7 (64 bit system) using PHP 5.3.5 it does not work (bearing in mind that I need to add a line
    PHP Code:
    date_default_timezone_set('UTC'); 
    for PHP 5.3.5.

    I have checked the PHP website and it appears that there are reported problems running strtotime on 64 bit systems.

    Can anyone help me with an alternative solution please?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    There are two paths you can take. The better one is to give strtotime a valid time and let it presume the date.
    855 is not a valid time. No, it isn't. But "0855" is.
    PHP Code:
    strtotime(sprintf("%04u"$xtime $interval)) 
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    Objection, you two

    strtotime() cannot produce valid results for an input value like 0864 (which could happen with that loop). strtotime would assume this figure to be a year number, which would result in a negative timestamp.

    strtotime() can parse those four figure values only if they appear to be correct time information. To solve that problem, you'd have to do sth. like this:

    PHP Code:
     strtotime(sprintf("%04u + %d minutes"$xtime$interval)) 
    Regards, Jens
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Originally Posted by JClasen
    strtotime() cannot produce valid results for an input value like 0864 (which could happen with that loop). strtotime would assume this figure to be a year number, which would result in a negative timestamp.
    That's exactly what I thought. Until I ran
    PHP Code:
    echo date("r"strtotime("0855")); 
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    0855 works - its 0861 and above, which doesn't. The loop will fail in the second run.

    Compare the date_parse() results for both examples to see what I mean.

    Regards, Jens

    Comments on this post

    • requinix agrees : er, right...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    72
    Rep Power
    6
    Thanks for your help. I have tried to "play" with sprintf but it still does not work on the Windows 7 system (64 bit).

    The code that I have used is

    PHP Code:
    $interval 9;
    $xtime "0846";
    date_default_timezone_set('UTC'); 
        for (
    $j=0;$j<20;$j++)
            {
    $xtime $xtime $interval;
    echo (
    "<br>".$xtime);
    $ytime sprintf("%04u"$xtime);  
    echo (
    " ".$ytime." ");
    $zxc date("Hi",strtotime(sprintf("%04u"$xtime)));  
    echo (
    " ".$zxc);


    On PHP 5.0 when the value of $xtime gets to 0964 strtotime converts it to 1004. That is what I am trying to achieve. The above code works on PHP 5.0 but it does not work with PHP 5.3.5 on a 64 bit processor.

    I simply want to loop round, adding 9 minutes to each time (24 hour format) and arrive at a new meaningful time.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    See the manual for further information about supported formats.

    Please take into account the given notes, such as:

    Note:

    The "Year (and just the year)" format only works if a time string has already been found -- otherwise this format is recognised as HH MM.
    So in your case, you used a design flaw in the strtotime() implementation. Don't rely on it to be working on all systems, with all libraries or in different php versions.

    For the solution to your problem: well, you might want to read #3 again...

    Furthermor you should use one of the supported time formats (HH:MM for example) - relying on yours is not a really reliable solution.

    Try this for example:

    PHP Code:
    $interval 9
    $xtime "08:46"
    date_default_timezone_set('UTC');  
    for (
    $j=0;$j<20;$j++) 

      
    $xtime date("H:i",strtotime("$xtime + $interval minutes"));   
      echo (
    " ".$xtime); 

    Regards, Jens
    Last edited by JClasen; March 14th, 2011 at 07:45 AM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    72
    Rep Power
    6
    Thanks very much Jens - works perfectly.

    The only change for my earlier code is that I have replaced

    PHP Code:
    strtotime($xtime $interval
    with
    PHP Code:
    strtotime("$xtime + $interval minutes"
    but I have checked in both PHP 5.0 and PHP5.3.5 and it works.

    Thank you very much.

IMN logo majestic logo threadwatch logo seochat tools logo