I have Datetime in integer in database.
Example. 1597192194
How can convert that in to date format dd-mm-yy h:i:s.
I have tried this one, not it's not working.
$this->db->select("DATE_FORMAT(created_on, '%Y-%m-%d %h:%i %p') as registered_on");
Please help.
Consider:
date_format(from_unixtime(created_on), '%d-%m-%Y %H:%i:%s') as registered_on
Rationale:
from_unixtime() converts your epoch timestamp to a datetime value
then date_format() applies the target format
Demo on DB Fiddlde:
select date_format(from_unixtime(1597192194), '%d-%m-%Y %H:%i:%s') as registered_on
| registered_on |
| :------------------ |
| 12-08-2020 01:29:54 |
Related
INSERT INTO covernote (issue_date) VALUES
( STR_TO_DATE ( '13-May-13','%Y-%m-%d') );
Because May is not %m see https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format - %b for abbreviated month or %M for full month name
+--------------------------------------+--------------------------------------+--------------------------------------+
| STR_TO_DATE( '13-May-13','%Y-%m-%d') | STR_TO_DATE( '13-May-13','%Y-%b-%d') | STR_TO_DATE( '13-May-13','%Y-%M-%d') |
+--------------------------------------+--------------------------------------+--------------------------------------+
| NULL | 2013-05-13 | 2013-05-13 |
+--------------------------------------+--------------------------------------+--------------------------------------+
1 row in set, 1 warning (0.106 sec)
It's a pity you picked may as a representative sample..
Per the documentation on date formatting, %m is the code for "Month, numeric (00..12)".
You are looking for %M, which represents "Month name (January..December)"
this is my query (Not Working): SELECT date_format('21-Oct-19 15:59','%Y-%m-%d %H:%i') + interval 3 day, date_format(now(), '%d-%b-%y %H:%i')
this is my query (Working): SELECT '2019-10-21 15:59' + interval 3 day, date_format(now(), '%d-%b-%y %H:%i')
My data is in this format 21-Oct-19 15:59 i want to add 3 day in it. How I can achieve this?
If you have a datetime in a non-standard format (that is, non YYYY-MM-DD HH:MM:SS), you need to convert it to a standard datetime using STR_TO_DATE().
You used DATE_FORMAT() which does the opposite — it only accepts a standard datetime, and you can format a non-standard datetime.
You also need a different format specifier corresponding to the non-standard datetime input. You don't need to specify the output format for STR_TO_DATE(). It always outputs standard datetime.
mysql> SELECT STR_TO_DATE('21-Oct-19 15:59','%y-%b-%d %H:%i') AS d;
+---------------------+
| d |
+---------------------+
| 2021-10-19 15:59:00 |
+---------------------+
mysql> SELECT STR_TO_DATE('21-Oct-19 15:59','%y-%b-%d %H:%i') + INTERVAL 3 DAY AS d;
+---------------------+
| d |
+---------------------+
| 2021-10-22 15:59:00 |
+---------------------+
You need to do the date_format after adding the interval :
SELECT date_format(now() + interval 3 day, '%d-%b-%y %H:%i') ;
I need to get the 24 hour time of a string, but I can only get the 12 hour for some reason using Mysql.
SELECT STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %T');
+-----------------------------------------------------+
| STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %T') |
+-----------------------------------------------------+
| 2018-03-13 09:28:07 |
+-----------------------------------------------------+
I have tried a variety of methods and thought it was working correctly, which it does, before noon....
I am trying to use it to limit the returned results to only things that have changed since the last time I ran the query.
%T is for time in 24 hour notation, so STR_TO_DATE is ignoring the PM/AM part of your time. You need to use %r. See the manual for details.
You need to convert it to datetime with time zone then use date format %T.
select DATE_FORMAT(STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %r'), '%T')
21:28:07
I have DATETIME column in my table, with 2015-04-23 11:17:49 properties
Trying to convert it to unix timestamp, acording to the mysql documentation I need just put the field into UNIX_TIMESTAMP() function and I'll get -> 1223423442 - timestamp but it's doesn't work, I've got only 0000-00-00 00:00:00
Tried a lot of stuff:
// doesn't work
UNIX_TIMESTAMP(CAST(`updated` AS CHAR(100))) AS updated_at,
// doesn't work
UNIX_TIMESTAMP(`updated`) AS updated_at,
//doesn't work
UNIX_TIMESTAMP(STR_TO_DATE(CAST(`created` AS CHAR(100)), \'%M %e %Y %h:%i%p\'))
AS created_at'
// doesn't work
UNIX_TIMESTAMP(STR_TO_DATE(`created`, '%M %e %Y %h:%i%p'))
AS created_at
Without `` doesn't work as well, am I missing something?
Try:
select
o1.id,
o1.operation_date_time,
(unix_timestamp(o2.operation_date_time) - unix_timestamp(o1.operation_date_time))
as duration
from operations as o1
inner join operations as o2
where o1.operation = "START"
and o2.operation = "STOP"
and o1.id = (o2.id - 1);
It should give as output:
+------+---------------------+----------+
| id | operation_date_time | duration |
+------+---------------------+----------+
| 1 | 2000-01-01 06:30:45 | 4455 |
| 3 | 2000-01-01 08:18:12 | 11146 |
| 5 | 2000-01-01 15:45:01 | 11792 |
+------+---------------------+----------+
3 rows in set (0.00 sec)
I do not understand why do you need to convert DATETIME to TIMESTAMP.
You can use INT(11) field to store UNIX TIMESTAMPs converted from DATETIME using function UNIX_TIMESTAMP(your_datetime_field).
Note, according to documentation: http://dev.mysql.com/doc/refman/5.5/en/datetime.html
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
I have a MYSQL table which has a column name called timestamp stored in string format. the timestamps in this column are in the following format e.g. '20/10/2014 05:39 PM'
Now how can select a row and convert the timestamp column to 24HR format on the fly.
basically I want something like this.
SELECT id, user, STR_TO_DATE(timestamp, '%d/%m/%Y %h:%i %p') as timestamp FROM mytable WHERE user="bob";
but this does not work. looks like its not recognizing the timestamp variable inside STR_TO_DATE sql function and its retuning NULL for the timestamp column.
Please help.
Looks fine to me:
mysql> SELECT STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p');
+---------------------------------------------------------+
| STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p') |
+---------------------------------------------------------+
| 2014-10-20 17:39:00 |
+---------------------------------------------------------+
(I know this should be a Comment, but is an Answer in order to get formatting.)
use date_format function instead of str_to_date:
SELECT id, user, date_format(timestamp, '%d/%m/%Y %h:%i %p') as timestamp FROM mytable WHERE user="bob";