How to change curtime? - mysql

How to change curtime value in MySQL, I run query SELECT CURTIME() and the result is 18:49:12.
I want to change the value to 17:49:12
If anyone knows a good way to do this I'd very much appreciate your help.

CURTIME() value is a value based on server timezone. You can change server timezone:
SET time_zone = 'America/New_York';
Or do this with MySQL function ADD_DATE():
SELECT SELECT DATE_SUB(CURDATE(), INTERVAL 1 HOUR)

You could be set the time-zone manually, like so:
SET GLOBAL time_zone = '-1:00';
or to set to your system time:
SET GLOBAL time_zone = SYSTEM;
See the documentation for more information.

Hum, I don't think you can do this. It would imply that mysql can change the system time !

If you are trying to get a different time zone, you could apply timezone OR if u always want to get 1 hour before current time you could reduce the current time by 1 hour

Related

Wrong timestamp in MySQL

I make an insert using now() as default, and when i look in the table it says for example 11:00, and when i click on it, it specifies the timezone (+3:00). Which means that it equals 8:00 UTC+0. Which is WRONG, because I actually made that insert at 11:00 UTC+0.
More strangely, when I try "SET time_zone = '+9:00'" or no matter which timezone I specify, it doesn't change ANYTHING - now() still creates the wrong timestamp with with UTC+3.
If I just write a single query "SET time_zone = '+3:00'; SELECT now()", it returns the correct value.
But if I write "SET time_zone = '+3:00'; update table set time=now() where id=11", and then check the table, the problem appears again.
I'm using 000webhost.
Please help? It drives me insane.

Can I change Mysql current time CURTIME() in query for testing purposes?

I have unit test, and I want CURTIME() always return same time, can I somehow do this ?
I can set timezone:
SET time_zone = 'America/New_York';
SELECT CURTIME();
But is there any way to set time ?
Part of my query is:
(CURTIME() BETWEEN
TIME(CONVERT_TZ(opening_time, timezone, "UTC"))
AND TIME(CONVERT_TZ(closing_time, timezone, "UTC")))
I want to test for more timezones and different times
CURTIME() is based on server timezone, thus the only way to make it change is directly changing the server timezone:
SET time_zone = 'France/Paris';
In debugging purposes, I would simply use #juergend idea to use a static time string to make your tests.
EDIT: didn't read well, you already knew that part. Still, I don't understand why you don't use a static time string in your query, no matter how many times you have to do it in the query
('12:34:56' BETWEEN TIME(CONVERT_TZ(opening_time, timezone, "UTC")) AND TIME(CONVERT_TZ(closing_time, timezone, "UTC")))

How to check server timezone

I want to know what is the time zone that is currently set in the MySQL server. I do not have administrator rights to the computer I am using so I've tried the method by checking the registry.
I am doing a table with a timestamp column and I noticed the time stamped is different than the one on my computer's time. Is there any reason for this? How do I check what timezone it is on the MySQL server? How do I change it to match my local/computer's time?
You can set the timezone (if you know your offset) for the session by using
set session time_zone = '+00:00';
and to revert to the system default
set session time_zone 'SYSTEM';
In an SQL timestamp column, SQL automatically converts the time to UTC before storing it, using the session's current time offset. It will be the machine's time offset unless you change it (3). Depending on your server's settings (sql.ini), it may or may not always concert back to the expect timezone. This probably explains the time discrepancy.
To get the current timezone offset, try executing
SELECT ##session.time_zone;
To manually override the SQL timezone for the rest of a particular session, execute the following, replacing 00:00 with your desired offset:
SET ##session.time_zone = "+00:00";
Have a look at the system_time_zone system variable.
This may help:
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
You can set the system time zone for MySQL Server at startup with the --timezone=timezone_name option to mysqld_safe. You can also set it by setting the TZ environment variable before you start mysqld. The permissible values for --timezone or TZ are system dependent. Consult your operating system documentation to see what values are acceptable.
You can convert a given timestamp to UTC (or any other TZ you want) with CONVERT_TZ
SELECT CONVERT_TZ(NOW(),##session.time_zone,'GMT');
Note that I use NOW() as simple demonstration, you would put in the timestamp you wanted to convert.
By the same token, you could convert a timestamp in your local TZ to the system
SELECT CONVERT_TZ($timestamp,'Your Time Zone' ##session.time_zone);
To check your shared server
<?php
echo date_default_timezone_get();
?>
To change
<?php
date_default_timezone_set("Africa/Addis_Ababa");
echo date_default_timezone_get();
?>

Overcoming timezone differences in MySQL Select

I am trying to select entries for a current date but cannot seem to get past the issue of a 1 hour time difference between my timezone and that of the server.
I was able to overcome this by using DATE_ADD() for adding entries but now I need to do the same to SELECT them. I tried inserting SET time_zone = 'America/New_York'into my MySQL connection command but it did not appear to change anything. I am using a PDO::QUERY statement, here is my connection command:
$db = new PDO('mysql:host=localhost;dbname=nightdes_points', $dbuser, $dbpsw);
What am I missing here? Thanks!
I would always store data in GMT0 and on insert/update/select I would always pass (converted) GMT0 date.

How can SELECT UTC_TIMESTAMP() return -10:00 UTC?

Either I'm being stupid or something's wrong here.
I have two SQL Servers, the one is on my local machine (local time +2 GMT) and the other is somewhere else (NOW() seems to return +8 GMT)and I access it through phpMyAdmin. I have a table that has a DATETIME column. I'm trying
to store the current GMT/UTC time and then display it again, still as GMT/UTC time.
Originally I stored DATE_SUB(NOW(), INTERVAL 8 HOUR) which worked just fine. However, then I read about UTC_TIMESTAMP() and liked it more, as it was shorter and the MySQL manual even said :
"The current time zone setting does not affect values displayed by functions
such as UTC_TIMESTAMP() or values in DATE, TIME, or DATETIME columns."
So perfect right? Except no.
Let's assume Current GMT is 2010-02-18 17:18:17 (I even double checked it with someone in Britain).
On my local (+2) server, I get the following results for the following queries:
SELECT NOW(); 2010-02-18 19:18:17
SELECT UTC_TIMESTAMP(); 2010-02-18 17:18:17
On my online server I get:
SELECT NOW(); 2010-02-19 01:18:17
SELECT UTC_TIMESTAMP(); 2010-02-19 07:18:17 (WHY?!)
Am I missing something?!
Probably because the clock are wrong on the online server?
Try running this:
SELECT ##system_time_zone, NOW(), UTC_TIMESTAMP()
and see which zone does it return and does it match the difference.