How to convert DateTime to a number in MySQL? - mysql

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

Related

How to find if entire week(s) past from creation on mySQL?

I am looking for a solution to find with mySQL every result that was created since a(or many) entire week(s) (7 days).
I tried this but it seems sometimes the result is false because is there more than one result per week.
SELECT *
FROM `datatable`
WHERE MOD(TIMESTAMPDIFF(DAY,UNIX_TIMESTAMP(created),NOW()),7)=0;
created is a timestamp.
Thanks for any response!
Try this:
SELECT
*
FROM
`datatable`
WHERE 1
AND `created` >= DATE_FORMAT(NOW() - INTERVAL 7 DAY,'%Y-%m-%d 00:00:00')
AND `created` <= DATE_FORMAT(NOW() - INTERVAL 7 DAY,'%Y-%m-%d 23:59:59')
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. 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, date may be a DATE string, a
DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or
YYYYMMDD. The server interprets date as a value in the current time
zone and converts it to an internal value in UTC. Clients can set their
time zone as described in
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html.
So, do not use UNIX_TIMESTAMP() function, but simple DATE().
As far as I can understand the question, you want the records that are are newer than a certainDate, AND not older than 7 days, or 14 days,..., 140 days (whichever of them is closest to the certainDate) from current date.
IF I understand you correctly, following might work for you with certainDate = '2013-04-01 00:00:00'
SELECT *
FROM `datatable`
WHERE `created` >= ADDDATE(
'2013-04-01 00:00:00',
INTERVAL MOD(ABS(TIMESTAMPDIFF(DAY, NOW(), '2013-04-01 00:00:00')),7) DAY
)
This query will fetch all results having created in last 14 days.

subtract 2 datetime in mysql ( one in 24 hours format and one in am/pm format )

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 to convert timestamp to datetime in MySQL?

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

MySQL: how do you convert a datetime to unix time?

can I do this is a query?
See the UNIX_TIMESTAMP() function.
This can be done in MySQL by using UNIX_TIMESTAMP() or UNIX_TIMESTAMP(date).
If called with no argument, this returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer.
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.
mysql> SELECT UNIX_TIMESTAMP();
-> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
-> 1196440219
use the UNIX_TIMESTAMP() function.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp

Creating DATETIME from DATE and TIME

Is there way in MySQL to create DATETIME from a given attribute of type DATE and a given attribute of type TIME?
Copied from the MySQL Documentation:
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'
To get a true DATETIME value from your two separate DATE and TIME values:
STR_TO_DATE(CONCAT(date, ' ', time), '%Y-%m-%d %H:%i:%s')
You could use ADDTIME():
ADDTIME(CONVERT(date, DATETIME), time)
date may be a date string or a DATE object.
time may be a time string or a TIME object.
Tested in MySQL 5.5.
datetime = CONCAT(date, ' ', time);
select timestamp('2003-12-31 12:00:00','12:00:00');
works, when the string is formatted correctly. Otherwise, you can just include the time using str_to_date.
select str_to_date('12/31/2003 14:59','%m/%d/%Y %H:%i');
Without creating and parsing strings, just add an interval to the date:
set #dt_text = '1964-05-13 15:34:05.757' ;
set #d = date(#dt_text) ;
set #t = time(#dt_text) ;
select #d, #t, #d + interval time_to_sec( #t ) second;
However this truncates the microseconds.
I agree with Muki - be sure to take account of time zones and daylight savings time!