This question already has answers here:
Convert Javascript time to MySQL format using PHP
(4 answers)
Closed 8 years ago.
I have a series of dates stored in my database from an import. They're in the format of
Tue 2 Sep 2014
What would be the best way to convert this to MySQL date format, for example, yyyy-mm-dd hh:mm:ss either using PHP or MySQL.
Thanks
It's easy, just use date() combined with strtotime()
$date = "Tue 2 Sep 2014";
$conv = date("Y-m-d H:i:s", strtotime($date));
echo $conv;
output:
2014-09-02 00:00:00
To omit the time just remove H:i:s.
You can use str_to_date function to convert a string to a date as
mysql> select str_to_date('Tue 2 Sep 2014','%a %e %b %Y') as date ;
+------------+
| date |
+------------+
| 2014-09-02 |
+------------+
You can also format the date as
mysql> select date_format(str_to_date('Tue 2 Sep 2014','%a %e %b %Y'),'%Y-%m-%d %H:%i:%s') as date ;
+---------------------+
| date |
+---------------------+
| 2014-09-02 00:00:00 |
+---------------------+
Check more about date_format and other date functions here
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Related
I have a mysql "users" table like this example:
id | user | created
100 | user001 | [27-01-2016 04:30 PM]
101 | user005 | [19-05-2017 09:28 AM]
102 | user019 | [09-10-2015 03:29 PM]
103 | user029 | [18-11-2017 05:40 PM]
And I want to get this table in descending order by created row like this:
id | user | created
103 | user029 | [18-11-2017 05:40 PM]
101 | user005 | [19-05-2017 09:28 AM]
100 | user001 | [27-01-2016 04:30 PM]
102 | user019 | [09-10-2015 03:29 PM]
I tried with DATE_FORMAT:
SELECT id,user,DATE_FORMAT(created,"%d %m %Y") AS created FROM users ORDER by created desc
But it shows nothing, how can I get it?
I'd like some help.
This works:
SELECT `id`, `user`, DATE_FORMAT(STR_TO_DATE(MID(`created`, 2, 19), '%d-%m-%Y %h:%i %p'), '%d-%m-%Y %h:%i %p')
AS `created:` FROM `users` ORDER BY STR_TO_DATE(`created:`,'%d-%m-%Y %h:%i %p') DESC
Here is a working SQL Fiddle.
Explanation:
The MID() MySQL function returns the middle part of a string, so we easily stripped out the []. After that, the STR_TO_DATE() function parses the returned string into DATETIME format (the %p is for AM/PM). The returning value is not properly formatted, at least not in the way you would wish to have it. So we then use DATE_FORMAT() to format the output. In the end, we use STR_TO_DATE() once again for the ordering since created: is returned as string.
I also changed your created output column to created: since it will not work if they are all the same name.
Your created date format isn't correct, you should alter the table and change the field from varchar/char(string) to timestamp type. I have already tried converting your string dates with 'STR_TO_DATE()' but it returns null because the format doesn't match either timestamp, date or time formats. Its always best practice to store dates/datetime as either date or timestamp data types. You can always apply formatting functions later in query to output dates to any desired format.
Looks like I am having a case of Monday morning!!!
Setup
As you can my local machine is in Eastern time zone with Day light Saving is in effect. That can be seen from 'date' command below.
date ; php -r 'echo mktime() .PHP_EOL ;'
Mon Apr 18 11:14:29 EDT 2016
1460992469
I then generated a unix timestamp using php. It is suppose to give your current time and convert that to Unix epoch at UTC 0:0:0 on Jan 1 1970.
My mysql Session is set to UTC, which I imagine 1460992469 represent as it is converted to UTC by mktime.
The Problem
The trouble is the conversion back to est does not recognizes Daylight Saving. Can anyone help to point the flaw in my logic.
SELECT CONVERT_TZ(FROM_UNIXTIME(1460992469), ##session.time_zone ,'EST') as converted_to_est , FROM_UNIXTIME(1460992469) , ##session.time_zone;
+---------------------+---------------------------+---------------------+
| converted_to_est | FROM_UNIXTIME(1460992469) | ##session.time_zone |
+---------------------+---------------------------+---------------------+
| 2016-04-18 10:14:29 | 2016-04-18 15:14:29 | UTC |
+---------------------+---------------------------+---------------------+
I tried using 'EDT' in CONVERT_TZ to no avail already .
Don't use 'EST'. Use 'America/New_York' (assuming United States).
In my database I have the date of each customer order stored in the format 02 Mar 2015
I have data from March and April and I want a query that will return just those 2 months. Once I have data for May it will return the three months etc.
The SQL syntax which I am trying to use is:
SELECT DISTINCT MONTH(DATE_FORMAT(date,'%d %b %y')) FROM orders
However this returns 0 rows. I presume this is an issue with date format.
EDIT:
Sample data from table:
id | date | time | order_id | item | quantity
1 | 02 Mar 2015 | 14:22 | 1029 | clasico | 9
1 | 05 Apr 2015 | 13:58 | 1029 | hindu | 10
try
SELECT DISTINCT MONTH(`order_date`) FROM `orders`
where order_date is the date field in orders
Note: The answer assumes the date column is a varchar rather than a datetime datatype.
The DATE_FORMAT() function is used to display date/time data in different formats. Note: This assumes the data is a datetime data type.
The STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments. See the code below in action in the SQL Fiddle demo.
select DISTINCT MONTH(STR_TO_DATE(date, '%d %b %y')) from orders
I have one field registered_date which is timestamp type in MYSQL. Now I an new with php I need your help.
I am getting this result: 2014-06-19 15:59:49
and I want this output: 19 June 2014 15:59 PM
Note: I am storing timestamp in php variable from sql query.
Any Idea?
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/pl/function.date.php
$date = date('D M Y H:i A', strtotime($timestamp));
Since you tagged the question with mysql here is the mysql solution, you can use date_format() function while selecting the data and you will have desired format.
mysql> select date_format('2014-06-19 15:59:49','%d %M %Y %H:%i %p') as date ;
+-----------------------+
| date |
+-----------------------+
| 19 June 2014 15:59 PM |
+-----------------------+
1 row in set (0.00 sec)
I'm trying to enter a date in a mysql table
`ssdate` datetime
The function that produces the date can output it in 3 formats:
1276142400000
Thu Jun 10 00:00:00 GMT-0400 2010
Fri Jun 4 2010
I'm wondering which of these would be easiest to convert to this field format?
Since I'm trying to save only the date, is there a better option than datetime that would work with one of these output formats?
You can use the third format:
date('Y-m-d H:i:s', strtotime('Fri Jun 4 2010'));
Just put the result in your datetime field. If you're going to use a date field instead you can do
date('Y-m-d', strtotime('Fri Jun 4 2010'));
The easiest way would probably be FROM_UNIXTIME(), but 1276142400000 does not appear to be a Unix timestamp:
mysql> SELECT FROM_UNIXTIME(1276142400000);
+------------------------------+
| FROM_UNIXTIME(1276142400000) |
+------------------------------+
| NULL |
+------------------------------+
1 row in set (0.00 sec)
Perhaps it's a Unix timestamp mutiplied by 1000:
mysql> SELECT FROM_UNIXTIME(1276142400000/1000);
+-----------------------------------+
| FROM_UNIXTIME(1276142400000/1000) |
+-----------------------------------+
| 2010-06-10 06:00:00 |
+-----------------------------------+
1 row in set (0.05 sec)
And, if unsure, you always have STR_TO_DATE():
mysql> SELECT STR_TO_DATE('Fri Jun 4 2010', '%a %b %e %Y');
+----------------------------------------------+
| STR_TO_DATE('Fri Jun 4 2010', '%a %b %e %Y') |
+----------------------------------------------+
| 2010-06-04 |
+----------------------------------------------+
1 row in set (0.00 sec)
You can set your column as a varchar(14), will work perfectly with your first format output.
In database I would store time values in in DATETIME field, mainly because of built-in methods for date manipulations (INTERVAL, etc.). One possible alternative is to store UNIX timestamp as numeric value, but I wouldn't recommend that. If in doubt, choose standard solutions.
As for date format to convert from, I would definitely go with UNIX timestamp (#1 option in your case, multiplied by 1000 I guess) as most universal one. All other formats are locale- and timezone-dependent, which is a possible source for a lots of headaches in the future.