SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP How do I show time difference?

    Hi everyone,
    The following code:
    PHP Code:
    <?php
    $con
    =mysqli_connect("localhost","root","root","db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }
    echo 
    TIMEDIFF('2013-07-12 10:00:00','2013-07-11 10:00:00');
    ?>
    Raises the following error message:
    Fatal error: Call to undefined function TIMEDIFF() in
    Any Idea what is wrong with how I use "TIMEDIFF" ?
    Thanks

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    4 Thread(s)
    Hi deotpit,

    There is no function TIMEDIFF in PHP, I expect the function you want is date_diff.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Hi deotpit,

    There is no function TIMEDIFF in PHP, I expect the function you want is date_diff.
    I might be confusing it with mysql "TIMEDIFF".
    In that case, how do i calculate time differenes using PHP? If I want to show the difference in hours between 2013-07-26 10:00:00 and 2013-07-26 10:30:00, how do I do it?
    Thanks

  4. #4
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by deotpit View Post
    how do i calculate time differenes using PHP? If I want to show the difference in hours between 2013-07-26 10:00:00 and 2013-07-26 10:30:00, how do I do it?
    Check the answer to this question on Stack Overflow, it gives an example of how to get the difference between two dates/times.

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Quote Originally Posted by fretburner View Post
    Check the answer to this question on Stack Overflow, it gives an example of how to get the difference between two dates/times.
    OK, I got it. not what I expected (I'm used to SQLServer) but it seems I can go on from this point.

  8. #8
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Check the answer to this question on Stack Overflow, it gives an example of how to get the difference between two dates/times.
    It was also the second result in google for a search for "time difference php".

    deotpit: is it really that hard to use google?

  9. #9
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    It was also the second result in google for a search for "time difference php".

    deotpit: is it really that hard to use google?
    I know where you're coming from, probably 90% of the questions that get asked here on the forum could be answered by searching google, but we can't just answer 'google it' all the time or it'd be a pretty empty forum.

  10. #10
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found answers in "sitepoint" forums more satisfactory then those at "Stack overflow"...

  11. #11
    Non-Member
    Join Date
    Jul 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There was also the second result in Google for a search for "time difference php".

  12. #12
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check your MySQL database..

  13. #13
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    663
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just to follow up a bit on what @AdMx said.

    The other responders showed you how to calculate a time difference using php functions. Which is perfectly understandable give that this is a php forum. However, if you want your database to do the calculations then just make a select statement and execute it.

    PHP Code:
    $sql "SELECT TIMEDIFF('2013-07-12 10:00:00','2013-07-11 10:00:00');"
    By doing this you can calc the difference between two datetimes columns without having to query the column values and then convert to php's format. Of course these sort of functions tend to be database specific so it can limit flexibility.

  14. #14
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    Just to follow up a bit on what @AdMx said.
    He didn't say much :-)
    My problem is solved whereby another post where fertburner suggested to change column type to "TIME". Only then the code you posted worked.
    It turned out to be a MySql problem but I didn't recognize it at the beginning. I've been doing MySql for 2/3 days only. Before that I was working with SQLServer where I didn't have to distinguish between t_sql and SQL. Now I'll have know to where HTML extends, PHP, MySQL not mentioning CSS Javascript and much more.. Knowing which forum to ask a question is a skill for itself.

  15. #15
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    Just to follow up a bit on what @AdMx said.

    The other responders showed you how to calculate a time difference using php functions. Which is perfectly understandable give that this is a php forum. However, if you want your database to do the calculations then just make a select statement and execute it.

    PHP Code:
    $sql "SELECT TIMEDIFF('2013-07-12 10:00:00','2013-07-11 10:00:00');"
    By doing this you can calc the difference between two datetimes columns without having to query the column values and then convert to php's format. Of course these sort of functions tend to be database specific so it can limit flexibility.
    It's also slower to do statements like this in mysql, because mysql will not use it's query cache when you're using time and date functions in select statements.

  16. #16
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use

    PHP Code:
    <?php
    $datetime1 
    date_create('2013-07-12 10:00:00');
    $datetime2 date_create('2013-07-11 10:00:00');
    $interval date_diff($datetime1$datetime2);
    echo 
    $interval->format('%R%a days');
    ?>

  17. #17
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brainworkindia View Post
    you can use

    PHP Code:
    <?php
    $datetime1 
    date_create('2013-07-12 10:00:00');
    $datetime2 date_create('2013-07-11 10:00:00');
    $interval date_diff($datetime1$datetime2);
    echo 
    $interval->format('%R%a days');
    ?>
    Thanks a lot brainworkinindia. That is very helpful. Ofcourse i'll need to change the format definition to get it in time.

  18. #18
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    663
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    It's also slower to do statements like this in mysql, because mysql will not use it's query cache when you're using time and date functions in select statements.
    Interesting. The mysql manual seems to disagree: http://dev.mysql.com/doc/refman/5.7/...operation.html

    I'd be a bit surprised to find out that querying to datetime values and then computing the difference in php is faster then doing it in mysql.

    However, I'd be completely shocked to find out their either approach would make a bit of difference at the application level.

  19. #19
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    Interesting. The mysql manual seems to disagree: http://dev.mysql.com/doc/refman/5.7/...operation.html

    I'd be a bit surprised to find out that querying to datetime values and then computing the difference in php is faster then doing it in mysql.

    However, I'd be completely shocked to find out their either approach would make a bit of difference at the application level.

    Ah, actually you're right :-)

    There are plenty of date related functions that will have this impact though:
    CURDATE()
    CURRENT_DATE()
    CURRENT_TIME()
    CURRENT_TIMESTAMP()
    CURTIME()
    NOW()
    UNIX_TIMESTAMP() with no parameters

    Worth knowing :-)


Tags for this Thread

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
  •