我尝试过各种方法来使时间戳在午夜至第二天更改,但是我没有任何运气。我正在使用Wordpress,并将默认位置更改为“山区时间”。我认为这两个可以一起交流,但我没有运气。
<?php current_time(timestamp); echo date('l, F jS Y'); ?>
<?php ini_set('date.timezone', 'America/Denver');
echo date('l, F jS Y'); ?>
我正在尝试的第一个,第二个片段是我以前使用的。
很难调试它,因为我必须等到晚上才能查看日期是否早于第二天。不是最佳的。感谢您的帮助。
答案 0 :(得分:0)
似乎您的服务器中的时区配置不正确。只需输出完整的日期和时间,您无需等待午夜即可获得正确和错误时间之间的时差。
关于php中的时间和时区,您必须知道两个事实:
知道这一点后,您可以轻松调试程序。
<?php
echo date('r').PHP_EOL;
// timezone not configured
// Wed, 02 Jan 2019 16:11:36 +0000
echo date('U').PHP_EOL;
// 1546445496
ini_set('date.timezone', 'America/Denver');
echo date('r').PHP_EOL;
// Wed, 02 Jan 2019 09:11:36 -0700
echo date('U').PHP_EOL;
// 1546445496 , same as above
从这一点出发,您有很多解决方案,我建议以下两种解决方案:
将此内容放入您的php.ini文件中:
date.timezone="America/Denver"
如果无法编辑php.ini,请使用DateTime对象手动将其转换。
$d = new DateTime();
$d->setTimezone(new DateTimeZone('America/Denver'));
echo $d->format('r').PHP_EOL;
// Wed, 02 Jan 2019 09:11:36 -0700
答案 1 :(得分:0)
函数返回准确的时区差异以及更新时间 包括日光节约。随时联系以进一步了解 协助。
/*
* @function minusonesecond return timezone
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function minusonesecond($timezonedifference=""){
$i=0;
$time = explode(":", $timezonedifference);
if($time[0]<0){$sign=-1; }else {$sign =1;}
foreach ($time as $endtmp){
$endtmp = $sign*$endtmp;
if($i>0){
$tmp[$i] = (($endtmp*1==0) ? 59: ($endtmp-1));
} else $tmp[$i] = ($endtmp-1);
$i++;
}
$ret = ($sign)* $tmp[0].":".(($tmp[1]>9)?$tmp[1]:"0$tmp[1]").":".(($tmp[2]>9)?$tmp[2]:"0$tmp[2]");
return $ret ;
}
/**
* @function getusertimezonedifference return timezone difference with UTC
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getusertimezonedifference($user_id = "", $changetoUTC=false, $flg_array=false)
{
$user = $this->User->find("first", array("conditions" => array("User.user_id" => $user_id)));
$timezone = $user["Timezone"]["timezone_name"];
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
/**
* @function getPSTtimezonedifference return timezone difference with UTC AND PST
* @param $changetoUTC optional
* @param $flg_array optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getPSTtimezonedifference($changetoUTC=false, $flg_array=false)
{
$timezone = "America/Los_Angeles";
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}