【问题标题】:Laravel convert timestamp into date with specific timezoneLaravel 将时间戳转换为具有特定时区的日期
【发布时间】:2019-10-26 15:06:02
【问题描述】:

我正在使用以下方法在 Laravel 中保存我的时间戳。

Carbon::now()->timestamp;

如何将时间戳转换为具有特定时区的特定时间格式(使用 Carbon)?

$unix_time_stamp = "1572095520";
$date = Carbon::createFromTimestamp($unix_time_stamp);
$date->setTimezone('Pacific/Auckland')->format('Y-m-d H:i:s');

我得到的结果不正确,应该是 10 月 27 日

【问题讨论】:

    标签: php laravel laravel-6 php-carbon


    【解决方案1】:

    请尝试以下方法。

    $unix_time_stamp = '1572095520';
    $converted = Carbon::createFromTimestamp($unix_time_stamp,'Pacific/Auckland')
        ->toDateTimeString();
    

    【讨论】:

      【解决方案2】:

      转到 config -> app.php 并更改 'timezone' => 'Pacific/Auckland',

      【讨论】:

      • 这将默认新西兰的时区,但是在我的问题上,用户有不同的时区。无论如何,我感谢你的努力,谢谢! :) 以上答案有效
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2020-03-17
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      相关资源
      最近更新 更多