Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime format with time zones

I am pulling date from API as a string in this format: yyyy-MM-dd'T'HH:mm:ss'Z'

The problem is when I try to save it, let's say today 2015-01-23T13:42:00Z the flags Z and T are not shown in MySQL database (the date is saved like this 2015-01-23 13:42:00).

I would like to keep the field date type, i.e. I don't want to save the date into varchar field.

I am not familiar with date timezone format, so any suggestions are welcome.

like image 735
Kristijan Iliev Avatar asked Jan 23 '16 12:01

Kristijan Iliev


People also ask

Does DateTime include timezone?

The DateTime structure. A DateTime value defines a particular date and time. It includes a Kind property that provides limited information about the time zone to which that date and time belongs.

How do you read a timestamp with time zones?

To calculate UTC time one has to subtract the offset from the local time, e.g. for "15:00−03:30" do 15:00 − (−03:30) to get 18:30 UTC. Show activity on this post. That timestamp has a timezone offset that is telling you what time it was and the UTC offset. With no offset it becomes 2017-02-03T14:16:59.094-00:00 .

What is Z in UTC format?

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "T0930Z". "14:45:15 UTC" would be "14:45:15Z" or "T144515Z". The Z suffix in the ISO 8601 time representation is sometimes referred to as "Zulu time" because the same letter is used to designate the Zulu time zone.

Is ISO 8601 always UTC?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .


1 Answers

First, read about the DATETIME, and TIMESTAMP types in the mysql docs.

Then, make a decision on which field type to use:

  • If you want to submit, store, and retrieve the same value without ever having MySQL perform time zone conversions, then use a DATETIME field.

  • If you want to submit and retrieve values to/from the field using a specific time zone, but actually store a UTC time, then use TIMESTAMP.

    • Consider that you might want to still use a TIMESTAMP type for UTC-based values, but just set the session time zone explicitly to UTC.
  • Never ever store date/time values in a VARCHAR field.

Finally, realize that there's a huge difference between what is actually stored in the database, versus what you see when you examine the data. The database is storing things efficiently, in a non-visible binary representation. You simply observe that value projected to a specific format during display. The format you use when you submit the data helps to craft the binary value, but then those strings are discarded. Later when you look at the field, if you print them to the screen then it converts back to a string so it can show you the value.

It's no different then if you sent an integer as "1,234" - it doesn't actually store that string, it stores 0x04D2 (or 0000 0100 1101 0010 in binary). You could read that back as "1234" or "1,234" and the formatting of putting the comma in the right place has nothing whatsoever to do with how the value was stored. It only has to do with how the tool that shows you the value renders it for output. The same thing is going on with your date formatting scenario.

like image 54
Matt Johnson-Pint Avatar answered Sep 29 '22 19:09

Matt Johnson-Pint