deotpit — 2013-07-26T07:31:23-04:00 — #1
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" ?
fretburner — 2013-07-26T07:50:25-04:00 — #2
There is no function TIMEDIFF in PHP, I expect the function you want is date_diff.
deotpit — 2013-07-26T07:57:42-04:00 — #3
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?
system — 2013-07-26T08:10:49-04:00 — #4
fretburner — 2013-07-26T09:22:09-04:00 — #5
Check the answer to this question on Stack Overflow, it gives an example of how to get the difference between two dates/times.
kylewolfe — 2013-07-26T09:42:33-04:00 — #6
deotpit — 2013-07-26T10:10:55-04:00 — #7
OK, I got it. not what I expected (I'm used to SQLServer) but it seems I can go on from this point.
system — 2013-07-26T10:36:59-04:00 — #8
It was also the second result in google for a search for "time difference php".
deotpit: is it really that hard to use google?
fretburner — 2013-07-26T10:54:38-04:00 — #9
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.
deotpit — 2013-07-26T11:01:23-04:00 — #10
I found answers in "sitepoint" forums more satisfactory then those at "Stack overflow"...
system — 2013-07-27T00:26:12-04:00 — #11
There was also the second result in Google for a search for "time difference php".
admx — 2013-07-29T20:35:05-04:00 — #12
Check your MySQL database..
ahundiak — 2013-07-31T11:53:36-04:00 — #13
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.
deotpit — 2013-07-31T13:16:28-04:00 — #14
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.
system — 2013-07-31T13:52:34-04:00 — #15
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.
brainworkindia — 2013-08-01T05:40:28-04:00 — #16
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');
deotpit — 2013-08-01T07:17:00-04:00 — #17
Thanks a lot brainworkinindia. That is very helpful. Ofcourse i'll need to change the format definition to get it in time.
ahundiak — 2013-08-01T10:10:33-04:00 — #18
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.
system — 2013-08-01T10:43:04-04:00 — #19
Ah, actually you're right
There are plenty of date related functions that will have this impact though:
UNIX_TIMESTAMP() with no parameters