PHP How do I show time difference?

Hi everyone,
The following 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

Hi deotpit,

There is no function TIMEDIFF in PHP, I expect the function you want is [fphp]date_diff[/fphp].

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

Check the answer to this question on Stack Overflow, it gives an example of how to get the difference between two dates/times.

http://php.net/manual/en/datetime.diff.php

OK, I got it. not what I expected (I’m used to SQLServer) but it seems I can go on from this point.

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.

I found answers in “sitepoint” forums more satisfactory then those at “Stack overflow”…

There was also the second result in Google for a search for “time difference php”.

Check your MySQL database…

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.


$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.

He didn’t say much :slight_smile:
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.

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.

you can use

<?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.

Interesting. The mysql manual seems to disagree: http://dev.mysql.com/doc/refman/5.7/en/query-cache-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 :slight_smile:

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 :slight_smile: