There is a manual page on Time and Date functions.
The best way to compare date and time and get minutes back is probably to use one of the following:
SELECT TIME_TO_SEC(TIMEDIFF(timea,timeb)+0)/60
SELECT TIMESTAMPDIFF(MINUTE,timeb,timea)
Note that while TIMEDIFF
works with times, TIMESTAMPDIFF
requires a date component - and the values are switched around for TIMESTAMPDIFF
.
You can return hours with either of these:
SELECT TIME_TO_SEC(TIMEDIFF(timea,timeb)+0)/60/60
SELECT TIMESTAMPDIFF(HOUR,timeb,timea)
Note that when using DATEDIFF
, the value returned is a difference between the date components of the given time - thus, a value of 23:59 on one day compared to a value of 0:01 on the next (a 2 minute difference) results in a date difference of 1 (a full day).
To get a difference between two times in 24-hour periods (length of a day) first calculate the difference in minutes then divide by the proper values to find the number of 24-hour periods. Try this:
SELECT TRUNCATE(TIMESTAMPDIFF(MINUTE,timeb,timea)/60.0/24.0,0)
This truncates the decimal portion, returning only the number of complete 24-hour periods in the specified timespan.
TIMESTAMP stores the number of seconds from 1970-01-01 00:00:01 to now. It automatically converts to the date and time format when you retrieve the data.
CURRENT_TIMESTAMP(): Returns the current date time with your timezone configured.
UTC_TIMESTAMP(): Returns the current date and time using UTC timezone.
If you're using a timezone that's not UTC, these functions always return different values, equal otherwise.
MySQL always stores the date and time in UTC so it can be easily converted to different timezones. NOW() or CURRENT_TIMESTAMP() return the date/time plus your timezone configured.
UTC_TIMESTAMP() just returns the date/time ignoring your timezone setting.
Best Answer
In MySQL any date/time values inserted into
timestamp
are automatically converted and stored as UTC.To quote MySQL documentation:
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions.
So the answer to your question would be to set the time_zone value for your connection to the database by setting the session variable called
time_zone
before inserting/updating the value into the database and do the same before fetching the value back from DB. By doing so, the conversion to the actual value of date/time to reflect the Timezone will be automatically handled by MySQL.This is how you can set the session Timezone on MySQL Client:
mysql> SET time_zone = timezone;
To set global Timezone on MySQL Client:
mysql> SET GLOBAL time_zone = timezone;
There will be ways to do this in the Programming Language you are using.
More details from MySQL manual here:
https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html