在Cakephp或php中将unix时间戳转换为gmt


convert unix timestamp to gmt in Cakephp or php

我正在开发Cakephp 2.x…我正在获取UNIX时间戳中的日期。。我想把这个Unix时间戳转换成GMT,然后以GMT格式保存到数据库中。。不知道如何在cakepp或php中做到这一点。。如果有人知道如何做到这一点,请与我分享代码。。我看过Cakephp的文档,但无法理解。。。所以如果你想粘贴文档的链接,请不要回答。如果有人不知道如何在cake中做到这一点,但在php中知道,那么它的是可以的

我为您编写了一个函数,该函数将时间戳转换为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());