Mysql database not showing the current date for NOW() function - mysql

I am using the NOW() with insert query so that I can get the currentTime when the query is triggered. But I get a time of 12 hours back in the database. How can I get the current Time ? Please help.

Perform this query right after you've been connected to the database:
SET time_zone = '+05:30';
Where it is your valid timezone offset in the quotes.

This might happen if the Timezone in your server configuration is different. Assuming that you are on a shared host, try adding the following lines when you are querying:
mysql_query("SET time_zone = '+05:30';");
Source: http://wiki.dreamhost.com/Running_web_scripts_in_your_Timezone
Else try adding the following to your .htaccess file
SetEnv TZ Asia/Colombo
A full list of all the Timezones http://www.php.net/manual/en/timezones.php

You can do something like this
SELECT * FROM tbl WHERE datetime < NOW() - INTERVAL 12 HOURS

I believe you can do, in your SQL query:
DATE_ADD(now(), INTERVAL 12 HOUR);
This will take the current time and subtract add 12 hours from to it.

Related

Why MySql is returning time in AM instead of PM?

When I run below query,
SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-%d %h:%i:%s %p')
it return time stamp as
2016-09-24 05:16:53 AM
but it is 05:16:53 PM in my PC.
I have run below queries as well to make sure time zone is set to SYSTEM
SELECT ##global.time_zone;
SELECT ##session.time_zone;
What might be the issue? I want to retrieve 2 hours old records but due to this issue I am stuck.
Following is my query,
SELECT * FROM myTable WHERE NOW() < DATE_ADD(my_field_time,INTERVAL 2 HOUR)
This won't work correctly as NOW is 12 hours behind.
Update: Mysql server is on my PC, version 5.5.28, SYSTEM time zone is reported by mysql server, OS: Windows 10
Try this
SET time_zone = yourtimezone //eg '+05:30'
After that try your query
SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-%d %h:%i:%s %p')
Please note SET time_zone = yourtimezone will work only for the current session.
To know the difference between current datetime in your session timezone and the current datetime in UTC,use the following query
SELECT TIMEDIFF(CURRENT_TIMESTAMP, UTC_TIMESTAMP)
If you need only specific workaround for your query as NOW is 12 hours behind,then try the query
SELECT * FROM myTable WHERE NOW() + INTERVAL 12 HOUR < DATE_ADD(my_field_time,INTERVAL 2 HOUR)

Get all the sessions 15 mins prior to start time

I'm trying to get all the session list who are going to start in 15 minutes. The problem is with client and server timezones and conversions. I've this code so far.
MySQL server is on a different machine and client (nodejs server) is running on another machine.
sql = 'SELECT * FROM sessions
WHERE FROM_UNIXTIME(?) BETWEEN SUBTIME(session.start, "0:15:0") AND session.start'
values = [Math.round(Date.now()/1000)] // convert milliseconds to seconds
I'm expecting something like this:
select s.*
from sessions s
where s.start >= now() and s.start <= date_add(now(), interval 15 minute);
One reason your query doesn't work is because between expects the first comparison value to be less than the second.

MySQL - NOW() different value if its in trigger or in select

I have an issue with MySQL NOW() function
if i run:
SELECT NOW();
i get the value:
2015-11-24 13:35:00 (correct value with Swedish winter-time)
But if i use NOW() in a trigger function to update a timestamp column when another column changes like this:
SET NEW.timestamp = NOW();
i get the value:
2015-11-24 12:35:00
How can i solve this or why is it behaving like this?
To get current date and time using NOW(), please set the timezone as per your country to get the correct date and time.
Please check the link here
If you want to change the MySQL server’s time zone to adjust the current date and time returned by the NOW() function, you use the following statement:
SET time_zone = your_time_zone;
MySQL has multiple ways of specifying a timezone - the server has a default time zone and each client can also specify their own time zone on a per-connection basis.
I think that your SELECT is using the time zone specified by your client connection, while the trigger is using the server's default time zone.
https://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html

MySQL updating and converting timezone

I have a database containing times (ex: 2013-07-10 23:25:36)
They're all in Mountain Standard Time (Calgary) and I need to convert them to UTC.
I've tried to use the following statement to do so, and it resets them all to
0000-00-00 00:00:00
UPDATE assets_time SET time=convert_tz(time, 'MST', 'UTC')
I would appreciate any advice, thanks
According to this article:
The value can be given as a named time zone, such as 'Europe/Helsinki', 'US/Eastern', or 'MET'. Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.
So this might be your problem. Also have you tried imputing numbers instead? Like this for example:
mysql>UPDATE assets_time SET time=CONVERT_TZ(time,'-07:00','+00:00');
You must use the standardize format:
UPDATE assets_time SET time=convert_tz(time, 'US/Mountain', 'UTC')
SELECT DATE_ADD(NOW(), INTERVAL -7 HOUR);
General syntax
SELECT DATE_ADD(NOW(), INTERVAL HOUR);
http://www.worldtimebuddy.com/utc-to-pst-converter
since you need for MST it is "-7"

What's the difference between a TimeStamp field and mysql curdate

I was working and my friend told me to use curdate() on mysql query to get the current date of the server... And I told him that I was using Time_Stamp field for date/time.
Now I start to think, is there a huge difference between this two ways ? One is better than the other? Or there is something that makes it a not good practice ? Also there is a now() that can be used too. I just wanted to understand how does it work or wich one is the best and why.
Short version:
NOW() = CONCAT(CURDATE(), ' ', CURTIME());
CURDATE() = DATE(NOW());
Some explanation:
NOW() gets both date (CURDATE()) and time (CURTIME()).
So if we do it the other way round, CURDATE() = DATE(NOW()).
Regarding timestamp, in MySQL Data Types we can see timestampis 3 bytes, while datetime is 8 bytes.