【问题标题】:convert unix timestamp to gmt in Cakephp or php [duplicate]在Cakephp或php中将unix时间戳转换为gmt [重复]
【发布时间】:2013-07-01 17:23:55
【问题描述】:

我正在使用 Cakephp 2.x ..我正在获取 UNIX 时间戳中的日期 ..我想将此 Unix 时间戳转换为 GMT,然后想以 GMT 格式保存到数据库中..不要知道如何在 cakephp 和 php 中做到这一点..如果有人知道如何做到这一点,请分享代码..我已经看过 Cakephp 的文档但无法理解这一点......所以如果你想粘贴文档链接,请不要回答。如果有人不知道如何在 cake 中执行此操作,但在 php 中知道,则可以

【问题讨论】:

  • @AmalMurali 我还没有尝试...不知道如何开始...我正在从 android 发布数据,其中我在 webapp 的 unix 时间戳中的变量中获取日期.. 所以不知道怎么办

标签: php cakephp datetime timestamp cakephp-2.1


【解决方案1】:

我给你写了一个函数,它将时间戳转换为 MySQL 期望的 GMT 时区中 DateTime 字段的格式。

function format_timestamp($unixtimestamp, $format='Y-m-d H:i:s', $timezone = 'GMT'){
    $DateTime = new DateTime;
    $DateTime->setTimestamp($unixtimestamp);
    $DateTimeZone = new DateTimeZone($timezone);
    $DateTime->setTimezone($DateTimeZone);
    return $DateTime->format($format);
}

echo format_timestamp(time());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 2017-01-18
    • 2013-12-14
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    相关资源
    最近更新 更多