UNIX_TIMESTAMP() function in MySQL gives the unix timestamp of NOW.
But I want to get the start unix timestamp of current hour.
is there any way to get it?
Suppose, the current time is = 2016-03-07 13:05:23
UNIX_TIMESTAMP() -> 1457334323
I want to get the unix timestamp of 2016-03-07 13:00:00
Expected Result:1457334000
Try this:
select UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d %H-00-00'));
You can use UNIX_TIMESTAMP() function to do the job done.
SELECT (
UNIX_TIMESTAMP(NOW()) - (MINUTE(NOW()) * 60 + SECOND(NOW()))
) AS startUnixTimestampOfCurrentHour;
Explanation:
Suppose, Now() returns 2016-03-07 13:05:23.
Now if you look closely then you just need to avoid the minute and second part. Minute and Second are expected to have zero value.
So if you subtract the minuteand second from now then you will reach to the start of the current hour. And then take the UNIX_TIMESTAMP of this particular time.
OR
SELECT (UNIX_TIMESTAMP() DIV 3600)*3600 AS startUnixTimestampOfCurrentHour;
select unix_timestamp(date_format(now(), "%Y-%m-%d %H:00:00"));
You could use DATE_FORMAT to remove minutes/second part:
SELECT UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00')) AS result
SqlFiddleDemo
Related
What is the date_trunc('minute', timestamp) equivalent in MYSQL? Thanks in advance
I like to use date_format() for this:
date_format(timestamp, '%Y-%m-%d %H:%i:00')
Or you can use date arithmetics (this works only if our initial value has no fractional seconds):
timestamp - interval extract(second from timestamp) second
One method is to convert to seconds, divide by sixty, truncate, and convert back:
select from_unixtime(floor(unix_timestamp(timestamp) / 60)*60)
What is handy about this is that it can be used for other intervals, such as every 15 minutes (or whatever) by tweaking the logic.
This is the simplest solution I've found.
MySQL MINUTE() returns a MINUTE from a time or datetime value.
MINUTE(time1)
In Mysql, we have a function `
curdate()
` which returns today's date.
i.e when execute
select curdate();
it returns date as,
2018-06-22
I want to append the time stamp to this date like
hh:mm:ss
How can I do it? Please help me.
I want to query database with attached timestamp.
something like this,
select * from user where user_created_on >= (curdate() 00:00:00)
timestamp is
00:00:00.
Just concatenate time section:
SELECT * FROM user WHERE user_created_on >= CONCAT(CURDATE(), ' 00:00:00');
select * from user where user_created_on >= getdate()
or
select * from user where user_created_on >= now()
both of these will have that timestamp with hours,mins, and seconds
ps: there is a curtime() that gets the timestamp as well as convert(varchar, curdate(), 108) will too :)
if you want all hours of the day you can use
where data_inicio between (CONCAT(CURDATE(), ' 00:00:00')) and (CONCAT(CURDATE(), ' 23:59:59'));
I have a date time field in a MySQL database and wish to output the result to the nearest hour.
e.g. 2012-04-01 00:00:01 should read 2012-04-01 00:00:00
Update: I think https://stackoverflow.com/a/21330407/480943 is a better answer.
You can do it with some date arithmetic:
SELECT some_columns,
DATE_ADD(
DATE_FORMAT(the_date, "%Y-%m-%d %H:00:00"),
INTERVAL IF(MINUTE(the_date) < 30, 0, 1) HOUR
) AS the_rounded_date
FROM your_table
Explanations:
DATE_FORMAT: DATE_FORMAT(the_date, "%Y-%m-%d %H:00:00") returns the date truncated down to the nearest hour (sets the minute and second parts to zero).
MINUTE: MINUTE(the_date) gets the minute value of the date.
IF: This is a conditional; if the value in parameter 1 is true, then it returns parameter 2, otherwise it returns parameter 3. So IF(MINUTE(the_date) < 30, 0, 1) means "If the minute value is less than 30, return 0, otherwise return 1". This is what we're going to use to round -- it's the number of hours to add back on.
DATE_ADD: This adds the number of hours for the round into the result.
Half of the hour is a 30 minutes. Simply add 30 minutes to timestamp and truncate minutes and seconds.
SELECT DATE_FORMAT(DATE_ADD(timestamp_column, INTERVAL 30 MINUTE),'%Y-%m-%d %H:00:00') FROM table
soul's first solution truncates instead of rounding and the second solution doesn't work with Daylight Savings cases such as:
select FROM_UNIXTIME(UNIX_TIMESTAMP('2012-03-11 2:14:00') - MOD(UNIX_TIMESTAMP('2012-03-11 2:14:00'),300));
Here is an alternate method (1):
DATE_ADD(
tick,
INTERVAL (IF((MINUTE(tick)*60)+SECOND(tick) < 1800, 0, 3600) - (MINUTE(tick)*60)+SECOND(tick)) SECOND
)
If you don't need to worry about seconds you can simplify it like this (2):
DATE_ADD(
tick,
INTERVAL (IF(MINUTE(tick) < 30, 0, 60) - MINUTE(tick)) MINUTE
)
Or if you prefer to truncate instead of round, here is simpler version of soul's method (3):
DATE_SUB(tick, INTERVAL MINUTE(tick)*60+SECOND(tick) SECOND)
EDIT: I profiled some of these queries on my local machine and found that for 100,000 rows the average times were as follows:
soul's UNIXTIME method: 0.0423 ms (fast, but doesn't work with DST)
My method 3: 0.1255 ms
My method 2: 0.1289 ms
Ben Lee's DATE_FORMAT method: 0.1495 ms
My method 1: 0.1506 ms
From How to round a DateTime in MySQL?:
It's a little nasty when you do it with datetime data types; a nice candidate for a stored function.
DATE_SUB(DATE_SUB(time, INTERVAL MOD(MINUTE(time),5) MINUTE ),
INTERVAL SECOND(time) SECOND)
It's easier when you use UNIXTIME timestamps but that's limited to a 1970 - 2038 date range.
FROM_UNIXTIME(UNIX_TIMESTAMP(time) - MOD(UNIX_TIMESTAMP(time),300))
Good luck.
To round down to the current hour, select:
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(column_name) / 3600) * 3600).
The value is expressed in the current time zone doc
This will return the next hour, that is '2012-01-02 18:02:30' will be converted into '2012-01-02 19:00:00'
TIMESTAMPADD(HOUR,
TIMESTAMPDIFF(HOUR,CURDATE(),timestamp_column_name),
CURDATE())
Instead of CURDATE() you can use an arbitrary date, for example '2000-01-01'
Not sure if there could be problems using CURDATE() if the system date changes between the two calls to the function, don't know if Mysql would call both at the same time.
to get the nearest hour would be:
TIMESTAMPADD(MINUTE,
ROUND(TIMESTAMPDIFF(MINUTE,CURDATE(),timestamp_column_name)/60)*60,
CURDATE())
changing 60 by 15 would get the nearest 15 minutes interval, using SECOND you can get the nearest desired second interval, etc.
To get the previous hour use TRUNCATE() or FLOOR() instead of ROUND().
Hope this helps.
If you need to round just time to next hour you may use this:
SELECT TIME_FORMAT(
ADDTIME(
TIMEDIFF('16:15', '10:00'), '00:59:00'
),
'%H:00:00'
)
I think this is the best way, since it also will use the least amount of resources-
date_add(date(date_completed), interval hour(date_completed) hour) as date_hr
I'm trying to make a query which brings back results based on a timestamp, say an interval of 30 minutes.
So what I figured out is that I can
SELECT * FROM x WHERE ts BETWEEN timestamp(now()-3000) AND timestamp(now())
So this will query everything from x with timestamps in column ts within the last 30 minutes.
However, this only works after now() is past the yyyy-mm-dd HH:30:00 mark because anytime before it will result in NULL... this is rather cumbersome and I don't understand why it won't just subtract the friggin minutes from the hour!
Please help me out! I couldn't find any other method of doing a query within the last 30 minutes, that is what I'm trying to achieve.
Best regards,
John
SELECT * FROM x WHERE ts BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND timestamp(NOW())
SELECT * FROM x WHERE ts BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
SELECT * FROM x
WHERE ts BETWEEN TIMESTAMPADD(MINUTE, -30, NOW()) AND NOW();
First of all you need to realize that timestamp() returns a unix timestamp in seconds. 3000 seconds is not 30 minutes, it should be 1800 seconds. try that
For me, what worked is following query
select * from x where (now() - ts) < 1800000
1800000 is 30 minutes, because 60000 ms is 1 minute
You'll have to use DATE_ADD() and DATE_SUB() operators for dates. Take a look at the documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
SELECT * FROM (`yourdb`) WHERE `timestamp` BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
hopefully this is an easy one.
I have a query that I want to produce results for todays date only based on a column (record_date) that uses CURRENT_TIMESTAMP.
so my query goes...
Select columns FROM fields WHERE table.record_date = DATE_SUB(NOW());
This is throwing up an error... :(
Thanks for you help....So i tried....
SELECT * FROM daily_record WHERE record_date = CURDATE()
but it yielded no result.
Here is a sample of the data in the column i am searching...
2011-03-31 11:28:37,
2011-03-31 11:28:37,
2011-03-31 11:28:37,
.....
Does it matter that the time is also saved?
Is that what you want ?
Select columns FROM fields WHERE table.record_date > CURDATE();
DATE_SUB() is for subtracting an interval from a date in MySQL. You've got DATE_SUB(now()), but don't specify an interval
It should be something like
... DATE_SUB(now(), INTERVAL 5 DAY);
so MySQL's complaining about the unexpected ), because of the missing interval.
If you want to convert 'now' into a date, you can simply use CURDATE(), or DATE(now())