Reputation: 15
Background: I have a system that allows users to create todos. Currently, a cron job goes off every 12 hours and sends our reminder emails. The problem? Not every user is in the same timezone, so my 7am can be their 1am.
Objective: Allow users to choose their own timezone. Also, allow users to choose their own preferred time, based on 15 minute increments (Choose Hour and 00,15,30 or 45).
What I've got: I set a variable on my web app to match the email server's timezone (-0600). I have allowed users to submit their own timezone and two preferred email times. I'm in the middle of writing a script that checks to see if the current time matches a user's preferred sending time and to see if the user's timezone matches the system's timezone.
Background, Part II: When a user submits their timezone, it is at that point that they choose their two preferred times (all three of those pieces of info get put in to their own cell in the db).
Where I'm stuck: I think the best option would be to convert the preferred times ($etime1 and $etime2) to the system's timezone ($systz) when the user submits, so it's stored in the db as the system's timezone. This way, the calculation is done when they user submits their preferences (not very often), and not every time the cron job is run (very often - every 15 minutes). User's timezone is under the variable $uzertz.
So: User submits data and this is what I've got"
if ($usertz == $systz) {
*submit the $etime1 and $etime2 data as-is*}
else { *run some sort of script to convert $etime1 and $etime2 to the -0600
(system's timezone) equivalent, after which is should be submitted to the db in it's
converted version* }
I understand PHP offers ways to convert time from one timezone to another, but I've been having a hard time following how those scripts work, especially since the one's I've found are looking at system times, not user-submitted times.
Thanks!
Upvotes: 1
Views: 551
Reputation: 646
In the server you should always use GMT. It's the only way to have a consistent time measurement, think e.g. at the daylight saving time. By the way, to convert a time to other TimeZones you could use DateTime
<?php
$date = new DateTime($etime1, new DateTimeZone($usertz));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone($systz));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
You can find a list of all available timezones using the function DateTimeZone::listIdentifiers().
Example:
<?php
// Server date/time
$etime1 = '2012-11-22 08:00:00';
// Server timeZone
$date = new DateTime($etime1, new DateTimeZone('Asia/Jerusalem'));
// Converted date/time
$date->setTimezone(new DateTimeZone('America/Chicago'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
And the output is:
2012-11-22 00:00:00-06:00
Upvotes: 2
Reputation: 2272
You can use the DateTime
object provided in PHP 5.
This allows you to provide a DateTimeZone
object, when you create your DateTime.
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
You can also change the Timezone dynamically.
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
Upvotes: 0