0

I have looked at previous questions and nothing seems to fit what I require.

I am working on a scheduling system and now the client wants to add timezones.

So basically I need to be able to proform an action at a specific date and time.

Lets say someone chooses to have an alert at the following date/time:

25-12-2010 13:01:00 + / - their timezone

I need to be able to store this in mysql as an unix_timestamp, I have been looking at PHP's DateTime and DateTimeZone but this has confused me more.

I have looked at strtotime to also, i am just unsure of the best way to work out the date/time with the offset.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Kyle Hudson
  • 898
  • 1
  • 14
  • 26
  • @Kyle Hudson - store the time in your execution server time zone only, with a column to tell what is the diff between your client timezone with your execution server time zone, such as your client time zone faster 3600 seconds than your execution server, so stored -3600, and sum it up – ajreal Nov 16 '10 at 17:18
  • 1
    why cant you use a DateTime or Timestamp Column? Also, see here for an [example on using DateTime and DateTimezone](http://stackoverflow.com/questions/2095020/how-do-you-change-the-timezone-in-php-for-an-existing-timestamp/2095165#2095165). – Gordon Nov 16 '10 at 17:21
  • @Gordon +1, @ajreal: wouldn't it be +3600 ? – Kyle Hudson Nov 16 '10 at 17:27
  • @Kyle Hudson - is in reverse order, if your execution server is slower, you would need to sum the `-3600` so your script can run one hour ahead, and vice versus – ajreal Nov 16 '10 at 19:12
  • @ajreal: how do you mean? by execution server? its a cron which runs every min. Server is Europe/London 1926 (at time of writing this) – Kyle Hudson Nov 16 '10 at 19:27
  • @Kyle Hudson - let said 1926 at london, which is 1826 in Paris. If the stored time is 1926, sum with -3600 seconds is just nice to be 1826, correct? or i interpreted wrongly ? – ajreal Nov 16 '10 at 19:34

1 Answers1

4

Just to make sure we're on the same page, I just want to reiterate that a unix timestamp is merely the number of seconds since the "Unix Epoch" (January 1, 1970). Therefore simple math will work on unix timestamps.

There are two ways you could go about this; judging from your post you're confused as to which you'd like to use.

The first way would be the easiest (and most logical) way, and that is to store their offset (if you already have it, that is) and multiply that by 3600 (1 hour in seconds), and then add that value to the current unix timestamp to get their final time of running.

Another way to do it is to use the DateTime and DateTimeZone classes. How these two classes work, as shown here, is that you create two DateTimeZone objects, one with your timezone and one with theirs; create two DateTime objects with the first parameters being "now" and the second being the reference to the DateTimeZone objects above (respectively); and then call the getOffset method on your timezone object passing their timezone object as the first parameter, ultimately getting you the offset in seconds that can be added to the current unix timestamp to get the time that their job needs to run.

The second way seems much more complex for such an easy task if I do say so myself, so the first solution may suit your needs better. However, if you wish to have a more complete method, then using the DateTime and DateTimeZone would definitely be a possibility.

Quick note on strtotime: Strtotime is an easy opposite of the date() command, and won't be of any more use than a "tool" to achieve what you are looking for. It in and of itself will not convert or find offsets for you; it simply converts a formatted date and time into a unix timestamp.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145