* Created by PhpStorm.
* User: lxd
* Date: 2019/1/9
* Time: 13:25
* Comm:*/
classDtFormat
{protected$dt;protected $ttz; //目标时区
protected static$instance;privatefunction __construct( $ftz, $ttz )
{if( !$ftz || !$ftz instanceof DateTimeZone )
$ftz= new DateTimeZone( date_default_timezone_get() ); //默认来源时区
if( !$ttz || !$ttz instanceof DateTimeZone )
$this->ttz = new DateTimeZone( C('PARTNER_TIME_ZONE') ); //默认输出时区
else$this->ttz =$ttz;
$this->dt = new DateTime( 'now', $ftz );
}/**
* author liuxiaodong
* date 2019/1/11 15:19
* @param null $ftz 来源时区
* @param null $ttz 目标时区
* @return DtFormat*/
public static function getInstance( $ftz = null, $ttz = null)
{if( !$ftz && !$ttz )
$key= '0';else{
$ftz_key= $ftz instanceof DateTimeZone ? $ftz->getName() : null;
$ttz_key= $ttz instanceof DateTimeZone ? $ttz->getName() : null;
$key=md5( $ftz_key . $ttz_key );
}if( !self::$instance[$key] instanceof self ) {
self::$instance[$key]= newself( $ftz, $ttz );
}returnself::$instance[$key];
}public function show( $original, $formart = 'Y-m-d H:i:s')
{//将时间转成时间戳
$new_original = @($original + 0);if (strlen($new_original) !=strlen($original)) {
$new_original=strtotime($original);if (!$new_original)return$original;
}/**
* notice
* 时区不一致,同一个时间转换成时间戳是不一样的 !!
* $str = '2018-09-26 07:23:31';
var_dump( date_default_timezone_get() ); //UTC
var_dump( strtotime( $str ) ); //1537946611
date_default_timezone_set( 'PRC' );
var_dump( strtotime( $str ) ); //1537917811
**/$datetime= null;if( $this->dt->getTimezone()->getName() ==date_default_timezone_get() ) {
$datetime= clone $this->dt;
$datetime= $datetime->setTimestamp($new_original);
}else{
$tmpDt= newDateTime();
$tmpDt->setTimestamp( $new_original ); $datetime = DateTime::createFromFormat( 'Y-m-d H:i:s', $tmpDt->format( 'Y-m-d H:i:s' ), $datetime->getTimezone() );