Jump to content

Server timezone, user timezone, user date format ...


Recommended Posts

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);
	
}

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.