SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to calculate datetime difference between two datetimes

    Hi All,
    How can we calculate the difference between two dates including time. For example begin datetime is "2007-10-04 11:26:00" and end dateTime is "2007-10-04 11:50:00". What can be the best solution here. Thanks in Advance.
    Zia Awan

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,872
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since datetimes are stored internally as milliseconds since midnight 1st Jan 1970, subtracting one from the other will give the difference in milliseconds. Simply divide the result by the appropriate number to give the units that you require the answer in.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Since datetimes are stored internally as milliseconds since midnight 1st Jan 1970
    Actually, just plain seconds:

    Code:
     $ php -r 'echo gmdate("r",0), "\n", gmdate("r",60), "\n", gmdate("r",1000);'
    Thu, 01 Jan 1970 00:00:00 +0000
    Thu, 01 Jan 1970 00:01:00 +0000
    Thu, 01 Jan 1970 00:16:40 +0000
    Also, it looks like lots of other people have come up with date diff solutions. A quick look through some of those sites should hopefully get you on your way.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xiaawan View Post
    Hi All,
    How can we calculate the difference between two dates including time. For example begin datetime is "2007-10-04 11:26:00" and end dateTime is "2007-10-04 11:50:00". What can be the best solution here. Thanks in Advance.
    Just do it in a mysql query -

    SELECT DATEDIFF('2007-10-04 11:26:00','2007-10-04 11:50:00');

    outputs 0 days.

    SELECT TIMEDIFF('2007-10-04 11:26:00','2007-10-04 11:50:00');

    outputs -00:24:00

  5. #5
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks all of you guys,
    Searching on the web, I got the following solution that gives me the best solution
    http://www.gidnetwork.com/b-16.html
    Zia Awan

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Thats not the best solution.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Thats not the best solution.
    I agree this is not the best solution for everyone. But I required the difference in hours, minutes and seconds between two dates so it helps me, but you are right there may be some other solution helpful for everyone. So I am in the search process, if I get a better one, surely I will go for that , but at the moment it helps me.
    Zia Awan

  8. #8
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where are these dates coming from? is it user input or are they coming from a database?
    What is the context of this problem?

  9. #9
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by catweasel View Post
    Where are these dates coming from? is it user input or are they coming from a database?
    What is the context of this problem?
    Yes, these values are coming from Database,

    Actually there are three columns StartDate (DateTime), EndDate (DateTime) and Length (Time), and I have to calculate the difference in time between two dates. That was my actual problem. I was having difficulties to calculate the time difference as I was unable to find any built in function that would do the job for me so searching on net I found this page http://www.gidnetwork.com/b-16.html. So I got an idea and solved my problem. Do you think there can be a better way to solve this problem. Thanks in advance.
    Zia Awan

  10. #10
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xiaawan View Post
    Yes, these values are coming from Database,

    Actually there are three columns StartDate (DateTime), EndDate (DateTime) and Length (Time), and I have to calculate the difference in time between two dates. That was my actual problem. I was having difficulties to calculate the time difference as I was unable to find any built in function that would do the job for me so searching on net I found this page http://www.gidnetwork.com/b-16.html. So I got an idea and solved my problem. Do you think there can be a better way to solve this problem. Thanks in advance.
    I assume this is a MySQL database.

    MySQL has already anticipated this kind of problem and supplied you with the appropriate functions.. DATEDIFF and TIMEDIFF. You don't need (and shouldn't) try to solve this in PHP.. do it in the query.

    MySQL Date and Time Functions

  11. #11
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by catweasel View Post
    I assume this is a MySQL database.
    You are right, it's MySQL Database. I am not sure why I did not use MySQL command to solve my problem although you also mentioned this in your earlier post in this thread. So now I am using the following code.

    Code MySQL:
    SELECT TIMEDIFF('2007-10-05 12:10:18','2007-10-05 16:14:59') AS length;

    And it gives me the right output that I needed. Thanks for your help, in this scenario I see this one the best solution. Again thank you very much and the link you provided is also very helpful. See you next, have a nice time
    Zia Awan

  12. #12
    SitePoint Member volomike's Avatar
    Join Date
    Jan 2008
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    xiawann:

    If I have two DateTime() objects, and I want to see which one is older, I just do:

    $sDate1 = the date in 'Y-m-d H:i:s' format for the proper timezone
    $sDate2 = ditto, but a separate date you want to compare

    PHP Code:
    $d1 = new DateTime($sDate1);
    $d2 = new DateTime($sDate2);

    if (
    $d1 $d2) {
    echo 
    'd1 is older';
    } else {
    echo 
    'd2 is older';

    Also, you can set the timezone and do +/- by given factors. So, if I have a subscription timestamp for a website feature, and I store this in my database as '2007-12-07 01:13:55' (Y-m-d H:i:s format), and I only deal with GMT values, and I want to see if the 12 month subscription has run out, I could do:

    PHP Code:
    date_default_timezone_set('Europe/London');
    $dDate = new DateTime($sDateTimeFromDatabaseStoredInGMTFormat);
    $dDate->modify('+12 months');
    // note below I don't have to pass gmdate('Y-m-d H:i:s') because 
    // this is done for us when empty
    $dNow = new DateTime(); 

    if (
    $dDate <= $dNow) {
    echo 
    'user needs to renew';
    } else {
    echo 
    'user does not need to renew';



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
  •