The following code:
// Check connection
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" ?
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?
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.
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
My problem is solved whereby another post where fertburner suggested to change column type to "TIME". Only then the code you posted worked.
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
$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
There are plenty of date related functions that will have this impact though:
UNIX_TIMESTAMP() with no parameters
This topic is now closed. New replies are no longer allowed.