Hi guys i have been trying to get the difference between 2 datetime columns stored in phpmyadmin database. the columns are createddate which has current_timestamp as it’s default value and waitingtime. The problem is when i execute my php query it returns a negative value. How can i correct this to show the correct time difference.
I think it’s because the one of the datetime field which has been set to current timestamp post results in army time while the other has results in regular time.
So created time is posted as
2017-02-06 22:03:42
and waitingtime as
2017-02-06 10:16:22
Now i’m clueless on how to fix this.
Edit: Sorry, I’ve fixed the above code, the TIMEDIFF() and DATEDIFF() takes only two parameters. In your case, it’s createddate and waitingtime.
And, it’s always better to save the time in UTC, so that it also saves the time zone. If you’re facing the time zone issue, I guess you can only revert and fix them.
But, as long as you are using similar code and it runs on the server side, it always takes the same time zone.
Try the following code for your program and let me know if it works. (This might not be the best way to do this though)
SELECT log,
TIMESTAMPDIFF(year, createddate, waitingtime) AS YearDiff,
TIMESTAMPDIFF(month, createddate, waitingtime) AS MonthDiff,
TIMESTAMPDIFF(day, createddate, waitingtime) AS DayDiff,
TIMEDIFF(createddate, waitingtime) AS TimeDiff
FROM memberloaninfo;
Ok i am still getting the time difference as 11hrs 47 mins instead of 13mins 40 sec. Not sure whats happening there.
Maybe if i can find a way for createddate to be entered into the database as 12hr am/pm time would be great or by changing waitingtime as 24hr army time.
Either way would be good but not seeing a way in myphpadmin for this to happen.
This is my table setup in myphpadmin for both time fields
Name Type Null Default
createddate datetime No CURRENT_TIMESTAMP
waitingtime datetime Yes NULL
But the difference between the two times you posted in #4, 22:03:42 and 10:16:22, is 11h 47m, it’s not 13 minutes. You’re either going to have to specify AM/PM in your time, or use 24h clock instead. A datetime column definition in MySQL uses 24h clock.
Yeah i was definitely using the wrong format didn’t even notice until i went thru the code. Was using date(‘y-m-d h:i:s’); instead of date(‘y-m-d H:i:s’). Thank you for your help.