Last active
December 11, 2015 03:38
-
-
Save suin/4538775 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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