Andy_Kemp Posted January 13, 2016 Share Posted January 13, 2016 I dont know if its right way to do this. If you would do it in a different way pleas let me know. Create unix timestamp with admin selected timezone else use server default timezone. function create_timestamp($config) { $time_zone = ($config['site_time_zone'] === 'default') ? date_default_timezone_get() : $config['site_time_zone']; $date = date_create(NULL, timezone_open($time_zone)); return date_timestamp_get($date); } Format unix timestamp to user selected timezone and date format else use default timezone and date format. function format_timestamp($config, $ui, $timestamp) { $time_zone = (is_logged_in($ui) && $ui['time_zone'] !== 'default') ? $ui['time_zone'] : date_default_timezone_get(); $date_format = (is_logged_in($ui) && $ui['date_format'] !== 'default') ? $ui['date_format'] : $config['default_date_format']; $date = date_create('@'.$timestamp); date_timezone_set($date, timezone_open($time_zone)); return date_format($date, $date_format); } Add interval to unix timestamp. function add_interval_timestamp($config, $interval, $timestamp = NULL) { $timestamp = ($timestamp === NULL) ? NULL : '@'.$timestamp; $time_zone = ($config['site_time_zone'] === 'default') ? date_default_timezone_get() : $config['site_time_zone']; $date = date_create($timestamp, timezone_open($time_zone)); date_add($date, date_interval_create_from_date_string($interval)); return date_timestamp_get($date); } Substract interval from unix timestamp. function sub_interval_timestamp($config, $interval, $timestamp = NULL) { $timestamp = ($timestamp === NULL) ? NULL : '@'.$timestamp; $time_zone = ($config['site_time_zone'] === 'default') ? date_default_timezone_get() : $config['site_time_zone']; $date = date_create($timestamp, timezone_open($time_zone)); date_sub($date, date_interval_create_from_date_string($interval)); return date_timestamp_get($date); } Quote Link to comment https://forums.phpfreaks.com/topic/300305-server-timezone-user-timezone-user-date-format/ Share on other sites More sharing options...
requinix Posted January 13, 2016 Share Posted January 13, 2016 If you're looking for a critique, use the OOP methods instead of the procedural functions. Like function format_timestamp($config, $ui, $timestamp) { $time_zone = (is_logged_in($ui) && $ui['time_zone'] !== 'default') ? $ui['time_zone'] : date_default_timezone_get(); $date_format = (is_logged_in($ui) && $ui['date_format'] !== 'default') ? $ui['date_format'] : $config['default_date_format']; $date = new DateTime('@'.$timestamp); $date->setTimezone(new DateTimeZone($time_zone)); return $date->format($date_format); } Create unix timestamp with admin selected timezone else use server default timezone.A Unix timestamp does not have timezones. It's the same number at the same time everywhere in the world. Your function does the same thing that a single call to time would. Quote Link to comment https://forums.phpfreaks.com/topic/300305-server-timezone-user-timezone-user-date-format/#findComment-1529603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.