【问题标题】:php datetime and zonephp 日期时间和区域
【发布时间】:2017-04-19 10:21:56
【问题描述】:

我正在尝试使用子午线将时间字符串转换为日期时间,但日期时间错误。
这是我的代码:

$timeZone = 'Europe/Warsaw';
date_default_timezone_set($timeZone);
$millis  = 1540399680000;
$seconds = $millis / 1000;
$date    = date("d/m/Y H:i:s a", $seconds);
debug($date);
$timeZone = new DateTimeZone('Europe/Warsaw');
$formato  = "d/m/Y H:i:s a";
$fecha    = DateTime::createFromFormat($formato, $date, $timeZone);
debug($fecha);
debug($fecha->format('Y'));
debug($fecha->format('a'));

调试结果($date)

24/10/2018 18:48:00 pm

调试结果($fecha)

object(DateTime) {
    date => '2018-10-25 06:48:00.000000'
    timezone_type => (int) 3
    timezone => 'Europe/Warsaw'
}

我期待的是 debug($fecha); date => '2018-10-24 06:48:00.000000'

【问题讨论】:

  • 你期望得到什么?
  • $fecha 是一个日期时间对象,你可以根据需要格式化。
  • debug($fecha->format('d/m/Y H:i:s a')); 它会给你想要的解决方案

标签: php datetime cakephp


【解决方案1】:

您是否尝试过使用CakeTime::format()?例如:

$date = CakeTime::format($seconds, 'd/m/Y H:i:s a', false, 'Europe/Warsaw');
debug($date);

【讨论】:

    【解决方案2】:

    根据文档 (http://php.net/manual/en/datetime.createfromformat.php),DateTime createFormFormat 函数返回一个 DateTime 对象。如果您想获得直接输出,请遵循此代码。

    $timeZone = 'Europe/Warsaw';
    date_default_timezone_set($timeZone);
    $millis  = 1540399680000;
    $seconds = $millis / 1000;
    $date    = date("d/m/Y H:i:s a", $seconds);
    debug($date);
    $timeZone = new DateTimeZone('Europe/Warsaw');
    $formato  = "d/m/Y H:i:s a";
    // instead of this
    //$fecha    = DateTime::createFromFormat($formato, $date, $timeZone);
    // add ->format in the end
    $fecha    = DateTime::createFromFormat($formato, $date, $timeZone)->format($formato);
    debug($fecha);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2013-09-15
      • 1970-01-01
      • 2014-11-10
      • 2012-07-18
      • 2017-04-18
      • 2017-01-10
      相关资源
      最近更新 更多