0

Working on a mod where a user inputs a datetime in this format "11/28/2012 11:34 am".

I am then using strtotime to convert it to a timestamp and saving it to my database.

I am then displaying it to the user in my template using the following date(DateTime, 'D M jS g:i A').

The problem is users who are not in the same timezone as the user who created the event do not get the correct time updated to reflect their timezone.

I have access to both timezone offset's of the user posting and the user viewing.

MY question is how should I compensate for timezone differences?

3
  • 1
    use GMT offset before converting. E.g. "11/28/2012 11:34 am +2 GMT" Commented Nov 28, 2012 at 17:26
  • EL, when I save it to the DB or when I display it to the user and where are you getting +2 GMT ? Commented Nov 28, 2012 at 17:28
  • @Residual Envy: both. 1st - you change usertyped date to "Greenwich Mean Time" (GMT0) and 2nd - while displaying to user with known time offset (GMT+[u_said_u_know_it]). Commented Nov 28, 2012 at 23:31

1 Answer 1

1

When converting the string to the real timezone use this to override server time zone with time zone of the user submitting the value: https://www.php.net/manual/en/function.date-default-timezone-set.php

When retrieving it later, use the same function to override server time zone to current user's one (the one that is appropriate for the user displaying datetime at the moment).

Also make sure you store it properly in the database: preferably using Unix timestamps within PHP, TIMESTAMP columns within database, FROM_UNIXTIME() database function when saving to the database and UNIX_TIMESTAMP() when retrieving from database (all are valid for MySql RDBMS, check your own DBMS if it is different).

Sign up to request clarification or add additional context in comments.

2 Comments

That might work except I only have access to the offset not the named timezone.
@ResidualEnvy: That will work if done correctly. Check time zones with names starting with "Etc/" on the list of available time zone identifiers in the "Others" section: us3.php.net/manual/en/timezones.others.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.