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.
Related
I need to store a deadline, that consists of a date and a time (e.g 2016-05-02 19:02). I am currently using a field that has DATETIME as datatype but the problem is that its automatically saving it as 2016-05-02 19:02:00.
as a solution i was thinking to save the date in a String field.
So i am wondering if i should do that ? any performance advantages/disadvantages ?
Don't fight the database. Use the builtin types unless you really need something that they can't offer(I'd say it's unlikely, though). (And by this I mean that you should use TIME, DATE or similar for times and dates. Then you can do calculations without having to convert values, etc)
If you don't need the seconds then just keep them 00 all the time.
Whether you use DATETIME, DATE & TIME or perhaps TIMESTAMP is up to you, how you use the data. Choose the alternative that makes most sense in your current situation.
As mentioned in the other answer, you should always use built-in data types whenever possible.
In your case, stick with DATETIME and then convert it to whatever format you need in the query using the DATE_FORMAT function, like so:
mysql> SELECT * FROM `mytable`;
+----+---------------------+
| id | mydatetime |
+----+---------------------+
| 1 | 2016-06-06 14:12:00 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_FORMAT(`mydatetime`,'%b %d %Y %h:%i %p') AS `mydatetime` FROM `mytable`;
+----------------------+
| mydatetime |
+----------------------+
| Jun 06 2016 02:12 PM |
+----------------------+
1 row in set (0.00 sec)
Reference:
http://www.w3schools.com/sql/func_date_format.asp
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
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
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)