This page explains how to format milliseconds
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now
but how do I get the actual millisecond value beyond just .00000?
I've tried these:
select unix_timestamp()+0;
select SYSDATE()+0;
select date_format(now(), '%f');
select now()+0;
but none of theme give me precise and accurate milliseconds
This is a bug from mysql http://bugs.mysql.com/bug.php?id=8523
If you want to get the microsecond from a field, use %f,
select FROM_UNIXTIME(DATE_COLUMN/1000,'%Y-%M-%d %H:%i:%s %f') from TABLE_NAME;
+-------------------------------------------------------+
| FROM_UNIXTIME(CREATETIME/1000,'%Y-%M-%d %H:%i:%s %f') |
+-------------------------------------------------------+
| 2016-March-18 16:02:54 342000 |
+-------------------------------------------------------+
Source : MYSQL DATE_FORMAT
Related
DB DETAIL Table name(KK)-
id name date(varchar(50))
1 Ayush 2020-04-19T18:56:09.774Z
I am using this query to convert -
update KK set date=DATE_FORMAT(STR_TO_DATE( KK.date, '%Y-%m-%dT%H:%i:%s' ), '%Y-%m-%d %T') where id=1;
Getting this error
Error Code: 1292. Truncated incorrect datetime value: '2020-04-19T18:56:09.774Z'
Your current date string has .774Z following the seconds, but your STR_TO_DATE() format string doesn't account for it. So it's warning you that there are extra characters at the end of the string that weren't parsed.
If you use '%Y-%m-%dT%H:%i:%s.%fZ' the warning stops.
The times have two parts your format is missing: milliseconds, the .774 part, and the "zulu" time zone Z.
select STR_TO_DATE('2020-04-19T18:56:09.774Z', '%Y-%m-%dT%H:%i:%s.%fZ');
However, since you're truncating them anyway, I'd use the less restrictive format and just ignore the warning.
Since the string is already in ISO 8601 format, you can skip the str_to_date.
mysql> select DATE_FORMAT('2020-04-19T18:56:09.774Z', '%Y-%m-%d %T');
+--------------------------------------------------------+
| DATE_FORMAT('2020-04-19T18:56:09.774Z', '%Y-%m-%d %T') |
+--------------------------------------------------------+
| 2020-04-19 18:56:09 |
+--------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
The MySQL warning is because it doesn't seem to understand that Z is a valid time zone designator.
Note that this will account for a time zone. This may or may not be what you want.
-- date_format will display in the current time zone.
-- I'm in -07:00, so a +00:00 (UTC) date is displayed -7 hours.
mysql> select DATE_FORMAT('2020-04-19T18:56:09.777+00:00', '%Y-%m-%d %T');
+-------------------------------------------------------------+
| DATE_FORMAT('2020-04-19T18:56:09.777+00:00', '%Y-%m-%d %T') |
+-------------------------------------------------------------+
| 2020-04-19 11:56:09 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
And, finally, consider altering that column to be a datetime type. Then everything is stored in UTC and these conversion problems go away.
When i SELECT ##system_time_zone it gives me output as named time zone instead of hours:
##system_time_zone
Arab Standard Time
I know that Arab Standard Time is +03:00 but i want output as +03:00 instead of named timezone.
May I ask for help from experts?
This is an example that provides you with the expected result:
-- set some time zone
set ##session.time_zone='Asia/Kolkata';
select concat( concat(if(timestampdiff(hour, utc_timestamp,current_timestamp)>0,'+',''),
timestampdiff(hour, utc_timestamp, current_timestamp)),
':',
MOD(timestampdiff(minute, utc_timestamp, current_timestamp), 60)) as zone_diff from dual;
+-----------+
| zone_diff |
+-----------+
| +5:30 |
+-----------+
You can set the default time Zone in PhP. Atfer that you can create an new date object which gives you the time in the TimeZone you set.
date_default_timezone_set("Europe/Berlin");
Look at this for more information: http://php.net/manual/de/function.date-default-timezone-get.php
Appreciate that this topic has been covered many times and I have tried all the combinations I can find without success.
The following timestamp is an example of that returned when using rpt_default_day.time_stamp:
1474502400000
If I put this time stamp into the following website it returns the correct date and time:
http://www.epochconverter.com/
Below are some examples of queries I have been using:
DATE_FORMAT(FROM_UNIXTIME('rpt_default_day.time_stamp'), '%e %b %Y') AS 'Date',
FROM_UNIXTIME('rpt_default_day.time_stamp') AS 'Date',
FROM_UNIXTIME(UNIX_TIMESTAMP('rpt_default_day.time_stamp')) AS 'Date',
Problem is whatever I do I'm always getting returned the epoch time of:
'1970-01-01 01:00:00.000000'
Appreciate any help in advance.
Remove three zeros from your string and you're good to go.
A proper format values should be passed as a parameter to "FROM_UNIXTIME" function :
mysql> SELECT UNIX_TIMESTAMP(NOW()) as ts,
FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()), '%Y %D %M %h:%i:%s %x') as f_tm;
+------------+-----------------------------------+
| ts | f_tm |
+------------+-----------------------------------+
| 1474755927 | 2016 25th September 01:25:27 2016 |
+------------+-----------------------------------+
1 row in set (0.06 sec)
possible format values are specified in the Mysql Documentation
I have a epoch number say 1389422614485.
The datatype for the value storing this value is varchar.
I want to convert its value to human readable time.
How can we do it?
Any example for this conversion?
Your epoch value 1389422614485 seems like having the millisecond precision. So you need to use some mysql mathematical functions along with from_unixtime() for generating human readable format.
mysql> select from_unixtime(floor(1389422614485/1000));
+------------------------------------------+
| from_unixtime(floor(1389422614485/1000)) |
+------------------------------------------+
| 2014-01-11 12:13:34 |
+------------------------------------------+
Update July 2020: As of MySQL 8.0, the floor function is no longer necessary when working with milliseconds:
mysql> select from_unixtime(1594838230234/1000);
+------------------------------------------+
| from_unixtime(1594838230234/1000) |
+------------------------------------------+
| 2020-07-15 18:37:10.2340 |
+------------------------------------------+
Take a look at from-unixtime
mysql> SELECT FROM_UNIXTIME(1196440219);
-> '2007-11-30 10:30:19'
You can use from_unixtime() as follows:
SELECT from_unixtime(1388618430);
which returns 2014-01-02 00:20:30
This wil work for both +positive and -negative epoch, in-case for old birth dates, and also if you want to specify date format
select
date_format(DATE_ADD(from_unixtime(0), interval '1389422614485'/1000 second), '%Y-%m-%d %H:%i:%s') as my_new_date;
how to change default date format when creating table in MYSQL
You can't change the default format for a date during the table definition stage. (It must always obey the DATETIME, DATE or TIMESTAMP formats.) As the manual puts it:
Although MySQL tries to interpret
values in several formats, dates
always must be given in year-month-day
order (for example, '98-09-04'),
rather than in the month-day-year or
day-month-year orders commonly used
elsewhere (for example, '09-04-98',
'04-09-98').
See the date and time reference docs for more info.
As such, you'll have to use the DATE_FORMAT() function at the point of output to achieve this goal.
You may want to use the STR_TO_DATE() and DATE_FORMAT() functions to communicate with MySQL using different date formats.
Example using STR_TO_DATE():
SELECT STR_TO_DATE('15-Dec-09 1:00:00 PM', '%d-%b-%y %h:%i:%S %p') AS date;
+---------------------+
| date |
+---------------------+
| 2009-12-15 13:00:00 |
+---------------------+
1 row in set (0.07 sec)
Example using DATE_FORMAT():
SELECT DATE_FORMAT('2009-12-15 13:00:00', '%d-%b-%y %h:%i:%S %p') AS date;
+-----------------------+
| date |
+-----------------------+
| 15-Dec-09 01:00:00 PM |
+-----------------------+
1 row in set (0.00 sec)