How to retrieve records with muliple timezones - php

I'm creating a site where i need to use registered users timezones to determine if they are available to receive emails on specific days based on what they set in their dashboard.
The main thing im wanting to tackle is that i would like the option of showing those users who are available NOW at the top of the results to save people having to search through pages of people to find those who are available.
I have a rough idea of a function i could use to show/hide the email form on the users page... Its more about the query on the search page that will display all the users and how best to order the results.
The default timezone will be GMT.
So if today was monday and the following users had their availability set as:
USER | timezone | availability_today | emails_received
1 | America/Los_Angeles | 2 | 1 (still available)
2 | America/New_York | 0 | 0 (not available)
3 | Europe/London | 1 | 1 (not available)
I think its more a question of what date/timezone I'm actually saving/accessing. In the email table id have a DATE_SENT column so i can count how many emails have been sent to compare against their availability. But if the site is GMT... that wouldn't be the users correct time if they were in the US or Australia...
Like i said, its more about the select statement on the search page that i cant figure out with all the timezones there would be. My host doesn't support CONVERT_TZ.
Or do i just calculate it all in GMT?
I'm just as confused asking the question as you are reading it im sure. Hopefully someone understands enough to offer a suggestion. Its probably not as confusing as im making it out to be.
EDIT:-
The issue:
Say its Monday GMT. In a users (not visitors) country its tuesday and they have their availability set as 0. If i use GMT its monday on the server so an email will get through. And on Tuesday (Weds in the users country) they wont get one.
Visitors wont have to register to view the search results. They will need to if they want to email a user. The users page is easy enough as its dealing with one conversion.

Couple of options...
(1) Store everything in GMT in datetime column. You can perform the manipulation on your end by performing MySQL queries such as
SELECT 'my date' - INTERVAL 5 HOUR
(2) Store everything in timestamp columns in the user's current timezone. You can set the timezone for the current database connection by executing the query...
SET time_zone = 'Europe/London'
View the current time zone with
SELECT ##time_zone;
Replace 'Europe/London' with the correct timezone. When MySQL uses timestamps it internally stores the data in GMT. When you pull the data back out, MySQL will automatically convert the time back into whatever timezone the client has currently set.

Store times in epoch time, converting for display only using FROM_UNIXTIME(). Forget multiple timezones, as Mr Sokolov so eloquently put it,
that way leads madness

Related

How to make a table in SQL that tracks people entered by the hour, but also stores the date?

I am using an HikVision Dual Lens People Counting Camera that tracks the amount of people that enter/exit a location by the hour. The data that is exported from the camera is automatically put into an excel file with the format being as follows:
Time Entered
People Entered
00:00:00.000
43
01:00:00.000
87
02:00:00.000
62
The file is then uploaded to my website and put into the database under the same format. However, it isn't good enough since I want to be able to query the data based on a specific day, for example how many people entered on every Monday, how many people entered from 2/10/2022 to 2/19/2022, etc. I have been trying to figure out a way for my table to have the date ALONG with the breakdown by hour, so for example:
2022-02-19 00:00:00
2022-02-19 01:00:00
2022-02-19 02:00:00
2022-02-19 03:00:00
My issue is, the software for the camera builds the excel sheet and conforms to the table above, and has no spot to include the date. I was wondering how I could get the date and the hourly breakdown of people entered all into one database so that I can query anything I need.
Sorry if this is a little incomprehensible, it's hard explaining my exact issue. I can provide updates if need be! Thank you so much for any and all help!!
If you cannot act on the system that generates the records in your database, a good option could be to add a date field to your table in order to get the day when the records were inserted.
ALTER TABLE mytable
ADD day DATETIME NOT NULL DEFAULT NOW();

How to manage date and time mismatch for different countries users in php and mysql

I am working on my social network project.
I'm so confused with the issue related with date and time of update, comment, messages and anything wherever I am showing date and time.
Suppose I am sorting my updates using date and time. I am storing date and time in mysql using CURDATE() and CURTIME() functions respectively.
Suppose, I just posted something in India on my profile at 6.40 pm. So when it shows on my London friend profile, it will show stored date and time means Indian date and time. But Indian time is ahead of the UK time. So it will show 6.40Pm for that update when current time of London is 2.10PM. So showing 6.40Pm which is not the good thing for them. So I have to show local time for that post means 2.10Pm.
Basically I want to show all updates time is less than current date and time of the user but it is impossible for updates posted by ahead time countries.
So how to store and convert these date and time according users countries?
What about saving the time in Timezone 0 (Greenwich time). Add a js file that changes the provided time to the browser time by adding the timezones difference.
You can search the timezones by country with DateTimeZone::listIdentifiers
Array
(
[0] => Atlantic/Azores
[1] => Atlantic/Madeira
[2] => Europe/Lisbon
)
you have to store user information based on location or else you cal retrieve location based LBS then set the timezone.
enter link description here

What is the best way to store Due Dates?

I have an application that needs to store due dates. So if Bob in the UK creates a task and says it's due on Feb 20, I want it to show as Feb 20 on the Calendar regardless of timezones of other users.
Storing it as UTC as DATETIME in MySQL presents the problem that if Bob created the task at 1am Feb 20, then Jane in California would see the task as due Feb 19.
So my other thought is to store it as DATE but I couldn't find any literature on how DATE is affected by the TimeZone.
So my question is, if my app is PHP and my storage is MYSQL and I want to implement Calendars that only store dates and not date/time and I want events to show as a certain date globally, how should I go about it?
Aside from my particular needs, I'm also curious on how other people implement date specific events (do you just let the date be different on different users calendars?) - For example how does Google Calendar/Outlook handle "all day events"?
You can just store the date as a DATE field. Since you want all users to see the same date there's no need to worry about timezones or the like. I.e. storing 2014-10-11 in the column and then showing that value to users.
The safest way to do this is to store both the unix timestamp and the timezone of the job creator. Then, when displaying the date for other users, use that timezone for displaying the time:
$timestamp=1413027859;
$timezone='Australia/Sydney';
$dt=new DateTime();
$dt->setTimestamp($timestamp);
$dt->setTimezone(new DateTimeZone($timezone));
$day=$dt->format('Y-m-d');

How to Ensure the Correct Sorting on the Base of Date and Time in MySQL

I am in a great confusion now. I have a comments table in mysql database. in comments table there is a field comment_posted_time (type DATETIME), I sort all the comments like this
$query = "SELECT * FROM comments ORDER BY comment_posted_time DESC";
Now, This can produce wrong results. I give you an example. Suppose there are two users sitting in front of computer, one in India and the other in America. Suppose India's Time is 10 hours forward from America. first the user from India posts a comment and its current local time is stored in comment_posted_time (type DATETIME). After 1 hour the user from America posts a comment and its current local time is stored in comment_posted_time (type DATETIME) as well. Now, in database the later posted comment from American User will not be counted the most current comment and sorting will not be correct.
How to handle this situation?
There is no difference where are your users, your php and mysql are in one server, so you must not have a problem with times, You can show times for each user by his timezone converting it by php, but don't change mysql default timezone for each user. Design your comments table and set on comment_posted_time default value NOW(), and don't use this field in your insert queries. So all records in that field will saved in same timezone.
How are getting the date for comment_posted_time? Typically this would be the current date on the MySQL server. If you're getting the datetime from the client machine (which I would NOT recommend), you could get the UTC date/time and then convert it back to the local time when you display it.
To store the current time from the MySQL server, you can do something like:
update comments set comment_posted_time = NOW() where id=...
This will not differ based on the client...
You can convert the date to its UNIX_TIMESTAMP and then sort by that.
$query = "SELECT * FROM comments ORDER BY UNIX_TIMESTAMP(comment_posted_time) DESC";
You can read more about UNIX_TIMESTAMP() documentation.
Edit:
You should not be storing the comment_posted_time relative to different timezones. All of the times should be relative to one timezone, like GMT, or the machine running php.
So if you're facing the problem, that your users are in different timezones you should ALWAYS calculate timepoints to one given timezone bofore persisting it (usually you take the main servers timezone, or UTC per default). With this approach you can do both: order by timepoint and show users the timepoint calculated to their timezone.

PHP and MYSQL time related query

I am developing a web application in which remainders will be sent to subscribers at a particular time of the day. Let's say a person John fills a form and asks to send email remainder at 12 pm every day. I have developed a cron job for this purpose but I am having issues related to time. Here is my problem if John is from USA then the email have to be sent according to US time and so on. Now I use MYSQL to store the time zone and Time from each person. But How do I schedule the emails according to specific time zone. My server time is set to UTC. So if a person has time zone of GMT +5 how many hours I have to add in the UTC so that emails should be sent at correct time.
I don't need the code just give me a common formula to calculate the correct time of email.
Thanks a lot,
Ahmar.
Take a look at the convert-tz function.
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
-> '2004-01-01 13:00:00'
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
-> '2004-01-01 22:00:00'
Don't calculate it, use the database. Mysql has the best timezone support of any of the mainstream RDBMS. Its even better than Oracle as far as having sensible ways of updating its database from Olson.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
Should give you a clue.

Categories