Calculate the difference between two timefields - php

Hi i am using third party API to display the feeds in our site, in that i have fields called opentime and closetime in db table. The feed table is being updated for every 20 seconds .Things working fine, now the problem is with opentime and closetime. Its giving 3 Hrs time in the future(it might be their server time) from the current time in my server. Say example, if my server current time is 8:00 AM, at the time feed table is being updated the opentime as 11:00 AM(3 Hrs from now, sometime its varying). We can't do any changes with feed table since the table is updating automatically through API call.
So i want to find out the time difference between these two(opentime - currenttime), how can i able to convert the opentime into my server time.
I tried the following code in my-sql query,
TIMESTAMPADD(MINUTE,-180,FROM_UNIXTIME(opendate) // subtracting 3 hours(180 min) from the opendate.
Is there any other way to do this?
Advance thanks for your guidance.

You can change the default time zone for MySQL. This can be set as a default for your MySQL server, or on the fly with each connection. Something like this:
set timezone='your timezone';
http://dev.mysql.com/doc/refman/5.5/en//time-zone-support.html

Set timezone for mysql config
set timezone='your timezone';
Try it
SELECT TIMESTAMPDIFF(MINUTE,'2012-10-21', NOW()) as diff;
diff=1997

Related

Some questions about EDT and time difference

Introduction to my website
My website is for visitors in Korea(AKA Republic of Korea).
And the server for My website is in the United States of America.
And PHPMyAdmin displays EDT when it runs a query SELECT ## system_time_zone.
Structure of my website
When I first uploaded my website to this server in October this year, I checked the DB time.
And it seemed that there was a time difference of 13 hours with Korea. So I added 3600 * 13 seconds to DB time(without setting timezone) as follows.
const Offset = 3600 * 13;
$SelectNow = $PDO->prepare('SELECT DATE_ADD(NOW(), INTERVAL '.Offset.' SECOND)');
$SelectNow->execute() or exit;
$DbNow = $SelectNow->fetchColumn();
My website takes $DbNow as above and uses it in various situations.
For example, in the posting situation, enter $DbNow in the datetime field of the INSERT INTO query as follows:
$WriteNote = $PDO->prepare('INSERT INTO table_note(my_datetime, my_contents) VALUES("'.$DbNow.'", :my_contents)');
$WriteNote->bindValue(':my_contents', $my_contents, PDO::PARAM_STR);
$WriteNote->execute();
The problem situation
One day in November of this year, when I wrote a post and checked the date field(my_datetime) of the post, I got an additional time difference of one hour with Korea.
Apparently, at the end of October, I corrected the time difference of 3600 * 13. And then I confirmed that it matches the Korean time. However, in November, There is a time difference of one hour!
Guess the cause
It seems that US summer time is being applied to the DB server of my website. Did I guess right?
My question
1) How can I solve this time difference fundamentally?
Is it correct to convert DB time to KST?
Or is it the correct way to convert to UTC and then added 3600 * x to UTC?
2) Even though the problem is resolved, some of the existing data in my DB has a time difference of one hour with Korean time.
What query do I use if I want to select the data with a time difference?
And how much more or subtract it from the data to get rid of the 1 hour time difference?
Use UTC to store time in Database.
change your queries to insert with UTC datetimes.
Use external libraries to convert UTC to respective timezones.
(below are the my personal recommendation.)
There may be best of it.
PHP : Carbon
Javascript : Moment, moment timezone.
No, it takes timezone of Database server resides in.
little manual verification, or create a job to change all dates in UTC.
Edit:
http://carbon.nesbot.com/docs/
I mean you can create a script and run with cron job.

How to add 1 hour to a queried time from the database?

I'm a beginner for php and developing this web application, which users who registered on this site, can be claimed some scores in every one hour. When a user claims at some time, database stores that time as time data type in to user_claim_time column. When that same user tries for his next claim, this php script be able to get his last claim time and add one hour to check if the user really claims in an one hour.
So, my question is how can we add one hour to queried time. I'm using php time(h:i:s) function to store server's current time into the database.
You can do something like this:
SELECT * FROM your_table
WHERE user_claim_time < NOW() - INTERVAL 1 HOUR
However i recommend you to use user_claim_time column in datetime format.
Because time like this '00:00:00' will produce negative output as one hour subtraction can change the date or month as well. For example date like this '2017-08-01 00:00:00'.
So using datetime is the right way i think to properly compare time difference.

MySQL delete older then * hours

I want to use the following code to SELECT data older then 1 minute.
SELECT * FROM `auth_temp` WHERE date > (NOW() - INTERVAL 1 MINUTE)
But it didn't work. Then I checked some other topics and one person talked about the server time, I just asked my host and he said the server time is: 15:30
When at my place and the logs in MySQL it is 21:30, aka 6 hours later.
Anyone how I should asjust my code to that?
Thank you all!
You are hitting a timezone issue. Most servers run on UTC. If you have a TIMESTAMP as the field type, MySQL will convert the time from server time to UTC and back. You can adjust what MySQL considers server time using SET time_zone = timezone; (Docs). If you actually care about timezones it is advisable to just use UTC and convert in your application.
Your current SQL statement will only select data newer than 1 minute. Change it to:
SELECT * FROM auth_temp WHERE date < (NOW() - INTERVAL 1 MINUTE)
This will select data that is older than one minute. If you are using NOW() for setting the date column when you are inserting the row then that small fix should do it even if the time zones are different between your application and database layer. If you are setting the date column from your application layer you will have syncing issues if the time zone is set differently than the database layer.
It sounds like the MySQL server is either running in a different time zone or running on Universal Time (UTC) which is common. Running MySQL on UTC time is a good way to deal with users in multiple time zones. In your code, you should be able to synchronize the time zones in use on the database and application layers if it's set to UTC time easily. If it's set to a different time zone, it should be possible as well but not recommended.

Timestamps are showing server time, not local time

I have a website where users(in different timezones) can create new records in a database, and i currently have each record being recorded with a timestamp of the time it was created... I am in EST time zone, but when i view the records that i have created on the site, they show the time in MST, which also happens to be the servers time zone... When i view the records in the database, the timestamp field is not converting to utc, it is instead converting to mst... I have noticed that when i insert a record from phpmyadmin, i use the datepicker and select 18:35:01, but when i look at the record after its been inserted, it displays 15:35:01!!! This is very frustrating, and i have been reading around that timestamp fields should automatically convert to utc, which mine is not doing... Another thing is, i am currently using a date function to format the timestamp, which is displaying in 00-00-00 00:00:00 format, and that was giving me errors for a while so i started using strtotime, which may be part of the problem... Im not sure... When records are inserted into the database i am using null for the value so it will automatically use current local time... But the thing is, different people from different timezones will be creating records than people who view them... So i need to acommodate for all of that... Any suggestions as to what i am doing wrong?
You can set the timezone used by your PHP script
date_default_timezone_set('America/Los_Angeles');
View the PHP Manual for more details

PHP Offset timezone times

I have written a PHP application and I've connceted to another database to display data.
In the database, the data says '2009-12-31 22:30:00'. Their website displays the data as 8:00pm (1.5 hrs off)
How do I convert 2009-12-31 22:30:00 to 8pm in PHP?
The field is $row['field_cck_monday_value']
The "cck" portion says that you're probably dealing with Drupal. Drupal has user-based date formatting and timezone conversion that it runs between the database and the view. Your best bet is to hook into that.
You can subtract required time from within your query something like this:
SELECT DATE_SUB(field_cck_monday_value, INTERVAL 90 MINUTE) FROM tbl
Or if you wish, you can set the appropriate time zone for your mysql connection or session.
$date = new DateTime(row['field_cck_monday_value']);
$date->sub($date, new DateInterval('P1.5h'))
I'm not certain on the DateInterval, exactly how to format the negative 1.5 hours

Categories