How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?
Use the FROM_UNIXTIME() function in MySQL
Remember that if you are using a framework that stores it in milliseconds (for example Java's timestamp) you have to divide by 1000 to obtain the right Unix time in seconds.
DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%Y-%m-%d %H:%i:%s') as "Date" FROM `orders`
This is the ultimate solution if the given date is in encoded format like 1300464000
To answer Janus Troelsen comment
Use UNIX_TIMESTAMP instead of TIMESTAMP
SELECT from_unixtime( UNIX_TIMESTAMP( "2011-12-01 22:01:23.048" ) )
The TIMESTAMP function returns a Date or a DateTime and not a timestamp, while UNIX_TIMESTAMP returns a unix timestamp
You can use
select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;
For in details description about
from_unixtime()
unix_timestamp()
SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
This work for me
Related
I have: (this example works)
SELECT *
FROM messages m
WHERE
m.create_timestamp > STR_TO_DATE('2016-01-28 05:06:38', '%Y-%m-%d %H:%i:%s')
ORDER BY m.create_timestamp
LIMIT 100;
but I have this '2016-01-28 05:06:38' datetime in UNIX FORMAT.
I tried:
m.create_timestamp > FROM_UNIXTIME(1453946798, '%Y-%m-%d %H:%i:%s')
But this doesn't work.
I want to compare date in UNIX FORMAT: m.create_timestamp > 1453946798.
Help please!
UNIX format is just the number of milliseconds since 1970-01-01 00:00:00 UTC. So you can just compare the values between them. The highest value will be the point that is later in time.
A simple:
m.create_timestamp > 1453946798
> 1453946798 means it's after 2016-01-28 05:06:38
< 1453946798 means it's before 2016-01-28 05:06:38
Make sure the unix timestamps have the same precision though. Sometimes it's the number of seconds since epoch instead of milliseconds for precision and storage size reasons.
If you have a timestamp in milliseconds to compare with your 1453946798, divide it by 1000 and floor it before comparing the timestamps.
To convert a DATETIME to a TIMESTAMP, you can use the following:
UNIX_TIMESTAMP('2015-01-15 12:00:00');
Of course, replace '2015-01-15 12:00:00' with the date to convert.
Try this
SELECT * FROM messages m
WHERE
m.create_timestamp > FROM_UNIXTIME(create_timestamp)
ORDER BY m.create_timestamp
LIMIT 100
I think you want unix_timestamp(); this command will convert a datetime to a unix timestamp: unix_timestamp('YYYY-MM-DD HH:MM:SS').
On my database under the t_stamp columns I have the date for example
2013-11-26 01:24:34
From that same column I only want to get the date
2013-11-26
How can i do this? Thank You!
You can use date(t_stamp) to get only the date part from a timestamp.
You can check the date() function in the docs
DATE(expr)
Extracts the date part of the date or datetime expression expr.
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
You can convert that time in Unix timestamp by using
select UNIX_TIMESTAMP('2013-11-26 01:24:34')
then convert it in the readable format in whatever format you need
select from_unixtime(UNIX_TIMESTAMP('2013-11-26 01:24:34'),"%Y-%m-%d");
For in detail you can visit link
To get only date from timestamp in MySQL just use DATE_FORMAT(datetime, '%Y-%m-%d'):
SELECT DATE_FORMAT('2013-11-26 01:24:34', '%Y-%m-%d');
> 2013-11-26
$date= new DateTime($row['your_date']) ;
echo $date->format('Y-m-d');
I used DATETIME datatype to store date and time in my Database. It saved date as
YY-MM-DD HH:MM:SS (2012-11-06 10:45:48)
I can retrieve the time from database as HH:MM:SS format but I want to retrieve it in HH:MM format.
How can I do this?
Check the DATE_FORMAT Function.
SELECT DATE_FORMAT(date_column, '%H:%i') FROM your_table
You will want to use DATE_FORMAT():
select date_format(yourDateColumn, '%h:%i') yourTime
from yourtable
See SQL Fiddle with Demo
SELECT DATE_FORMAT( NOW() , '%H:%i' ) AS time_column
I'm trying to create a query using mysql.
select ID,NCOde,ifnull(EndTime,now())-starttime from xxx where starttime between
'2012-05-09 00:00:00' and '2012-05-09 23:59:59'
the problem is ifnull(EndTime,now()) return datetime in 24 hours format, while the starttime using am/pm format.
I've tried using DATE_FORMAT(starttime, '%m-%d-%Y %T'), but it seems that the operation changed the datetime type to other type.
Any advice?
Use STR_TO_DATE() to convert your starttime string to a MySQL DATETIME:
STR_TO_DATE(starttime, '%m-%d-%Y %r')
and then use TIMEDIFF() to subtract two times:
select ID,NCOde,
TIMEDIFF(ifnull(EndTime,now()), STR_TO_DATE(starttime, '%m-%d-%Y %r'))
from xxx
where STR_TO_DATE(starttime,'%m-%d-%Y %r')
between '2012-05-09 00:00:00' and '2012-05-09 23:59:59'
You should probably consider changing the data type of the starttime column to DATETIME or TIMESTAMP. Note also that this assumes EndTime is already of such a data type, or else you will also have to perform a similar conversion with it too.
Use the DATE_SUB() function.
Plus what eggyal said.
How can I get the total number of seconds since '1970-01-01 00:00:01' from a DateTime instance in MySQL?
You are looking for UNIX_TIMESTAMP().
See: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC.
Use UNIX_TIMESTAMP( dateField )
SELECT DATE_FORMAT(`value`, '%Y%m%d') AS `date_ymd` FROM `table_name`;
UNIX_TIMESTAMP(datetime) force a localization of the datetime, which unlike the timestamp, is stored "as is".
You need actually any of the following, for discarding the UTC correction:
UNIX_TIMESTAMP(CONVERT_TZ(datetime, '+00:00', ##session.time_zone))
or:
TIMESTAMPDIFF(SECOND,'1970-01-01 00:00:00',datetime)
Refs: 1, 2, 3, 4