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 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php date time functions

    Hi,
    I have the following date time format:
    24-11-2010 11:6:57

    I want to get a beginning datetime and an ending datetime and caluculate the difference in hours. How can i transform the above date to a format that can caluslte? the stamp posted is from a calendar date picker.

    thanks in advance
    c.c.

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,379
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Change it to unix timestamp ....

    http://php.net/manual/en/function.strtotime.php

    The timestamp resolution is in seconds.

  • #3
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and the above format is acceptable so i dont have to change it?

    thanks

  • #4
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,379
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Correct ... your format is fine.
    The conversion (strtotime) is only for calculation.


    Like this example:
    PHP Code:
    <?php

    $time1
    ="24-11-2010 11:6:57";
    $time2="24-11-2010 19:4:35";

    // difference in seconds
    $diff=abs(strtotime($time1)-strtotime($time2));

    // difference in hours (rounded up).
    echo intval($diff/3600);

    ?>

  • #5
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    159
    Thanks
    37
    Thanked 2 Times in 2 Posts
    Good solution above....These post help me lot...........Thanks

  • #6
    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
    I'm a fan of the dateinterval myself. Hours is a pain though, so for that I'd still suggest sticking with strtotime (or massage the DateInterval which would be more work). It's a bit easier to use overall IMO.
    PHP Code:
    $time1= new DateTime("24-11-2010 11:6:57");
    $time2= new DateTime("24-11-2010 19:4:35");

    $diDiff $time1->diff($time2); 
    $diDiff is the daterInterval difference between the two. Best used for large dates, not small (it doesn't keep track of a total number of hours, only a total number of days).
    PHP Code:
    print('Difference is ' $diDiff->format('%y years, %m months, %d days, %h hours (total days: %a)')); 
    The diff on dateTime also has an inversion parameter to always force absolute.

    So with the smaller times, I would recommend you use simple subtraction (either using strtotime to get the integer, or by using DateTime and pulling the integer out) as indicated here. For anything larger, I'd suggest the DateInterval (to see how many years, months, days, etc ago). The DateInterval doesn't play great with hours, but can be easily calculated based on the total number of days past.

    And yes, I'm also aware that this thread is a couple of years old now, so the DateInterval would just *barely* have applied as it was released mid 2009.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,379
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Fou-Lu,

    I'm pretty much blown away at how much trouble PHP has at doing date functions. There are dozens of date functions to pick from, 'use this one', 'use that one', 'don't use that unless you do this', etc. Make sure your dates are formatted like YYYY-MM-DD for more accurate calculations, etc. How does anyone know which method to use and when to use it?

    What's happening? Does Perl, Java, C++, and all the others have the same issues with date/time calculations? Or is this something where PHP did it wrong from the beginning and now it has escalated into a whole fiasco of functions and tricks to make it work?

  • #8
    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
    IMO its something that the older PHP versions did wrong. Or its possible that there was no easy way to create backwards compatibility with the existing procedural code and so there was no option but to write another set of functions to handle them. Both are good possibilities.
    DateTime is 5.x functionality. The entire library is unstable between 5.0 and 5.3 where many new classes and methods were added. So the only frustrating thing is writing something on 5.3 which just doesn't work with say 5.2.8.
    strtotime is also instable though as indicated by the changelogs. <4.4, next was listed as +2, <5.0.2 now was incorrectly calculated from midnight, and < 5.1.0 returns -1 on failure instead of false which is just silly since -1 is a valid time.

    Compared to other languages, its hard to say. Most other languages I use you import to do many of the time manipulations. Before namespace (and even since due to backwards compatibility), php is stuck with only global symbols so it has dozens of functions for date and time.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    New Coder
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your mentioned date & time format is perfect. You can use this format any time.


  •  

    Posting Permissions

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