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 4 of 4

Thread: Date difference

  1. #1
    anu
    anu is offline
    New Coder
    Join Date
    Jan 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date difference

    I am running an application on PHP with MySQL. i want to check the difference of two dates. I want to get the difference in hrs:min:sec. My field type is DateTime. I tried with mktime etc but still i could n't get the right value. please help.
    Suppose date1 = 2006-01-01 10:05:00
    Date2= 2006-01-01 17:55:00
    i want the differnce between this?

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    PHP Code:
    $diff date("H:i:s", (strtotime($date2)-strtotime($date1)); 

  • #3
    anu
    anu is offline
    New Coder
    Join Date
    Jan 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank u for ur reply. but still it is not giving the correct value. i written as below
    $diff = date("H:i:s", (strtotime('2006-01-01 17:00:00')-strtotime('2006-01-01 10:00:00'));
    Is it correct? is there any other method? i am fetching this date value from table.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    I checked this out, and the difference in seconds is being done correctly, but the date(...) function cannot be used to get an HH:MM:SS that this corresponds to.

    The difference will be a number of seconds. When these are treated as a unix timestamp, they look like dates near January 1 1970 00:00:00 GMT. When the date(...) function is used, it gives the date/time that these small value timestamps correspond to relative to your current time zone. There will be an error in the number of hours that will be the difference between your time zone and GMT.

    If you use the gmdate(...) function instead of date(...) it should work (it did for me.)
    Last edited by CFMaBiSmAd; 10-27-2006 at 06:38 PM. Reason: fixed wording
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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