What is the best way to store Due Dates? - php

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');

Related

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

PHP/MySQL Timezone when working only with Date not Time

My question is more on the concept side.
I'm wondering how to deal with dates only based information. For example, when you have a day to take a vacation or something like that on February 24th and you store on the database as 2015-02-24 00:00:00 right.... how do I deal with conversions in this case?
I already now how to convert timezones, it works with no problem in cases where the user selects a time too. But when I use 00:00:00 in cases only the date matters I'm having problems to figure it out.
For example:
Let's say I setup my vacation to be on the 24th of February and I'm on EST right, and the system is saving all times on EST. It will convert and it will be saved as 2015-02-24 00:00:00
Then somebody on PST time will check it out, and when it convert to them it will be 2015-02-23 21:00:00 ... and it will show my vacation in their point of you it will be on the 23rd.
Is that a correct logic? How should I save on the database the time in that case? Keep it as 00:00:00?
I hope I was able to explain myself.
Thanks
You can save the date in unix timestamp in the database (always save the current time in GMT even if you are in EST).
And when displaying the date, convert it to the user's timezone.

compare date and time with users timezone php

I'm creating an auction site where time and date is inserted in following format:
Auction Date : 08/20/2015 # 05:30 PM IST
I want to match this time with my user's time and dont want to show auctions which have passed the start date as per users time zone. Assume users may come from different timezones.
Can someone please guide me on this.
You would have to storage your user's timezone somewhere for comparisson purposes. This means your user's model (user's specific info in your system) must know about their timezone.
Then in PHP you have the DateTime and DateTimeZone classes to play with it.
To give you an example, and this is in the docs you can instantiate DateTime like this:
$dt = new DateTime("your date string", new DateTimeZone("[timezone_location_string]"));
You can compare DateTime objects with the PHP comparisson operators (<,>,>=,<=,==) which is pretty nice.
And here you have a list of timezones used in PHP for instantiating objects like DateTimeZone():
http://php.net/manual/en/timezones.php

How to retrieve records with muliple timezones

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

How to deal with timezones between server and client?

I'm developing a website where I have to deal with different possible timezones from the users. This becomes a great problem since the website hosts time-delicate events like auctions.
All dates/times on the server are in UTC. Database stores everything in UTC timestamps. PHP default timezone is set to UTC too (date_default_timezone_set('UTC');).
Now, my problem is how I should interact with the users, whether I'm only showing a date or, more important, I'm reading a date/time from user input.
A concrete example:
An auction has a deadline, which I store in database as UTC.
When I view the auction on the website, a javascript timer uses a Date object to calculate the remaining time. It automatically converts the timezone to GMT+0100 (my local timezone). So if the deadline is '2013-08-08 10:46:08' (UTC), the javascript date object will return Aug 08 2013 11:26:15 GMT+0100 (GMT Standard Time).
If the current time is greater than 11:46:08 then the timer says that the remaining time is 00:00 (which is correct).
But if I try to insert a bid, the server accepts since the condition on the MySQL INSERT evaluates to true:
INSERT INTO Bids ... WHERE ... AND auction_deadline > NOW() ...
( because auction_deadline = '2013-08-08 10:46:08' and NOW() = '2013-08-08 10:26:50')
All this mumbo jumbo of timezone melts my brains. What am I missing here? I'm almost certain that storing all dates/times in UTC inside the database is the best. I just can't think crystal clear how do deal with it between the user and the database.
Your problem doesn't involve timezones at all, just the fact that clients can turn their clocks or have their clock skewed considerably. For that the fix is to poll the server every once in a while for an offset fix to use in calculations.
In fact, you don't even need date objects. There is a certain universal instant in time when the auction ends. Let's say it is 1375960662823. Right now, the universal instant in time is 1375960669199, so from that we see that the auction ends in 6 seconds (1375960662823 - 1375960669199 ~ 6000 ). It will end in 6 seconds regardless if I am in Morocco or Japan. Do you understand it yet?
To generate these numbers, on the client side you can call var now = Date.now() + skewFix where skewFix is the correction that needs to applied in case client has time skew or manually set their computer to wrong time.
In PHP, you can generate it with $now = time() * 1000;
This is rather a typical subject yet very complex for most to understand. First thing, you never mention the DAYLIGHT SAVING. yeah I am increasing your tension :).
Now let us see how we can do this. You did a good job by saving the Time in UTC. Now, I hope you have registered members and that each member has ability to set their preferred timezone, otherwise you will show Server' timezone based time to them.
When you through "start time" to user you must send them after converting UTC time to their time, similarly when you accept TIME from browser be it user action or javascript you need to convert that time to UTC considering the fact that user is that time zone that he select for his profile.
Hope that clear the idea on where you are going wrong? Please read through day light saving as that will play an important role too when you move ahead with other logic on same.
EDIT:
You can use javascript's Timezone offset, for auto submission and user input based on his settings.
Date in JavaScript uses local timezone. You should get UTC time for the user and send it to the server
new Date
Thu Aug 08 2013 17:00:14 GMT+0530 (India Standard Time)
(new Date("Thu Aug 08 2013 17:00:14")).toUTCString();
"Thu, 08 Aug 2013 11:30:14 GMT"
This will resolve the timezone issue between the server and client.
You said
( because auction_deadline = '2013-08-08 10:46:08' and NOW() = '2013-08-08 10:26:50')
In MySQL - NOW returns the current time in the server's local time zone (docs).
You probably want something like UTC_TIMESTAMP which returns the current time in UTC (docs).
Also - you probably shouldn't accept any input time from the client JavaScript at all. Only trust your own clock. When a bid is placed, use the time on your server in MySQL or in PHP. Don't accept it as input.
You can do the following
once page is loaded, send an ajax request to server with timezone offset of user. You can get timezone offset using the following code.
var curdate = new Date()
var offset = curdate.getTimezoneOffset()
offset is timezone offset in minute.
I think it will help.
everytime when you get the date from the clientside, you can use the getUTC to convert to UTC date ie:
var todayDate = new Date();
var todayDateInUTC = new Date(todayDate.getUTCFullYear(), todayDate.getUTCMonth(), todayDate.getUTCDate(), todayDate.getUTCHours(), todayDate.getUTCMinutes(), todayDate.getUTCSeconds());
so right before you insert the bid date to database, use the getUTC functions to convert it into UTC format.

Categories