Jump to content

Php Set Time Zone


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

Currently the user is inputting a timezone via GMT offset on my site. I realize that date_default_timezone_set() only accepts certain city names. Dates are coming out as GMT/UTC right now and it would be preferable to set the default time zone.Also this error is coming up because I don't have a time zone set:

Strict Standards: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for 'GMT/0.0/no DST' instead in /home/portal/public_html/SOMEPATH/someFile.php on line 101
How do I set time zone via GMT offset and not city names?
Link to comment
Share on other sites

If you have access to php.ini, set the date.timezone setting to UTC, like:

date.timezone = UTC

Alternatively (and I'm guessing this is what you want to), use date_default_timezone_set() with 'UTC', like:

date_default_timezone_set('UTC');

From then on, your app will have to do the UTC offset calculations. As you've already found out, timezones in PHP work with cities, not with UTC offsets.

Link to comment
Share on other sites

Guest FirefoxRocks

In this case, how should I store/access my dates from the database?Should I store everything as UTC and then convert that to the correct time zone?If so, do I use date()/mktime() or gmdate()/gmmktime() when I'm storing the date? When I'm retrieving the date?I guess I'll play around with the date functions and see which one does what.

Link to comment
Share on other sites

I'd suggest you redesign your application to take cities into account, instead of UTC offsets, store UTC times in the DB, and use the date/time functions (or the DB ones) to convert it to whatever city the user has specified.

Link to comment
Share on other sites

Guest FirefoxRocks

Well initially I had tried a design like that but it turned out to be overwhelming for the end user because the list was too big, even if I only include the Canada/USA time zone cities.Anyways, I'm forcing the application to use the UTC timezone, and then I have this function whenever I need to parse a date:

function formatDate($date, $format, $gmt_offset){	$year = substr($date, 0, 4);	$month = substr($date, 5, 2);	$day = substr($date, 8, 2);	$hour = substr($date, 11, 2);	$minute = substr($date, 14, 2);	$second = substr($date, 17, 2);		$timestamp = mktime($hour, $minute, $second, $month, $day, $year);	$gmtDiffSeconds = $gmt_offset*60*60;	$actualDate = gmdate($format, $timestamp+$gmtDiffSeconds);		return $actualDate;}

Link to comment
Share on other sites

JavaScript has this getTimezoneOffset() function. I don't know how it behaves with timezones with the same UTC hourly offset though.If it returns the offset of the same hourly offset differently (i.e. the minutes are different for each individual time zone), you can directly select a default choice for the user.If it returns the offset of the same hourly offset the same (i.e. the minutes for each hourly offset are equal), you can style the best suggestions specially (e.g. if all timezones have black text, the recommended ones may be green to signal they're more appropriate).Either way, this is an add-on, so accessibility isn't going to suffer.

Link to comment
Share on other sites

  • 4 weeks later...
Guest FirefoxRocks

I really have a problem with dates and times I guess. I suppose someone can fill this out to help me understand, I'm too confused. Fill in the blanks with either server time zone, GMT/UTC or CST.Document head: date_default_timezone_set("America/Rainy_River");User inputs a date + time in a form, submits it.The _____ date/time is stored in the database.So when I retrieve the date/time from the database, I use mktime() to get a timestamp in the ____ timezone. I use gmmktime() to get a timestamp in the GMT timezone, but this won't work if the database contains a GMT timezone and I have that line set at the top?After getting the timestamp, I use _________ to get the correct time in the CST time zone (gmdate or date)?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...