How to compare between different date formats?

Multi tool use
How to compare between different date formats?
I have four different date formats that I will store in a DB, Then show the latest ones.
The four different formats:
$a = '27. júní 2018 04:53';
$b = 'Friday, 09 March 2018';
$c = 'Fri, 29 Jun 2018 11:00:00 GMT';
$d = 'Mon, 18 Jun 2018 06:52:20 +0000';
They will be stored in a MYSQL Database.
What should I do with them?
Can SQL or MYSQL date type do the work?
Should I convert them using strtotime()
?
strtotime()
Should I extract some data from specific ones to male them match?
php.net/manual/en/function.strtotime.php
– WPZA
Jul 1 at 14:59
Convert as you store into the table.
– Rick James
Jul 2 at 1:52
@RickJames, You mean before storing it or after?
– Ashley
Jul 2 at 3:02
Store a
DATETIME
or TIMESTAMP
-- '2018-06-18 06:52:20`; no other form.– Rick James
Jul 2 at 3:20
DATETIME
TIMESTAMP
1 Answer
1
MySQL has three data types suitable for date/time values. These are pretty well explained in the documentation.
You have three types of values:
But the basic questions to ask:
If you need just the date, then date
will do. Otherwise, you want datetime
or timetamp
. Which depends on how you want to treat timezones.
date
datetime
timetamp
If you actually need to store the timezone with the value, then I would suggest that you think hard about whether this is really necessary. Typically, timestamp
is sufficient, because it stores values in UTC which can then be converted to any original timestamp. You can store the original time zone in an alternative column.
timestamp
If you really need to handle all three types with no compromise, then you might need to use a string. In this case, you should use a correctly formatted string:
The first two are readily converted to the appropriate type in MySQL. For the third, you can extract the timezone and add it as an offset (with a bit of work).
However, I doubt whether this approach is necessary. A datetime
is probably sufficient.
datetime
I want to store them for two reasons,:
– Ashley
Jul 1 at 11:59
1- Show each post creation date.
– Ashley
Jul 1 at 11:59
2- Fetch the latest added posts from the DB.
– Ashley
Jul 1 at 12:00
Timezone is not important, I can extract it from the dates.
– Ashley
Jul 1 at 12:00
@Ashley . . . Then you should use either
timestamp
or datetime
. You can use datetime
if the value is all from the same timezone, such as from the server time zone.– Gordon Linoff
Jul 1 at 13:14
timestamp
datetime
datetime
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can SQL or MYSQL date type do the work? Maybe you should try before asking the question. Please post the code that you have tried and explain which part is not working.
– Dvorog
Jul 1 at 10:38