Skip to content

Instantly share code, notes, and snippets.

@suin
Last active December 11, 2015 03:38
Show Gist options
  • Save suin/4538775 to your computer and use it in GitHub Desktop.
Save suin/4538775 to your computer and use it in GitHub Desktop.
<?php
namespace Goodby\TimeZone;
use Exception;
use DateTime;
use DateTimeZone;
class ReliableTimeZoneDateTime extends DateTime
{
public function format($format)
{
throw new Exception('You can\'t call format(). Please call by formatWithTimezone()');
}
public function formatWithTimezone($format, $timezone)
{
return (new DateTime)
->setTimestamp($this->getTimestamp())
->setTimezone(new DateTimeZone($timezone))
->format($format);
}
}
// Server: Asia/Tokyo
// User: Asia/Shanghai
if (true) {
define('ZEN_METRICS_CLIENT_TIMEZONE', 'Asia/Shanghai');
} else {
define('ZEN_METRICS_CLIENT_TIMEZONE', 'UTC');
}
// model
$createdTime = (new ReliableTimeZoneDateTime())->setTimestamp(999994149);
var_dump($createdTime->formatWithTimezone(DATE_ATOM, date_default_timezone_get()));
echo '✄ - - - - - - - - - - - - - - - - - - - - - - - -', PHP_EOL;
// view
var_dump($createdTime->formatWithTimezone(DATE_ATOM, date_default_timezone_get()));
var_dump($createdTime->formatWithTimezone(DATE_ATOM, ZEN_METRICS_CLIENT_TIMEZONE));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment