简体   繁体   English

将Unix时间戳转换为良好的GMT日期PHP

[英]Convert an Unix timestamp to good GMT date PHP

I want to get the FRENCH GMT DATE from an Unix timestamp. 我想从Unix时间戳中获取FRENCH GMT DATE。 In my database, i saved the date in GMT+0 then i get the timestamp of this date and want to display the date with the good GMT+2 在我的数据库中,我将日期保存在GMT + 0然后我得到了这个日期的时间戳,并希望显示具有良好GMT + 2的日期

My timestamp is :1461857633 from database and it's equal to : 29/04/2016 12:27:11 And now i want to display this date with local GMT. 我的时间戳是:1461857633来自数据库,它等于:29/04/2016 12:27:11现在我想用本地GMT显示这个日期。 So i did this : 所以我这样做了:

$timestamp = 1461857633;
$format = 'd/m/Y H:i:s';
$res = date($format, $timestamp);
echo $res;

and i have the same date 29/04/2016 12:27:11 where as my timezone is well 'Europe/Paris' Normaly i should to have this date : 29/04/2016 14:27:11 我有相同的日期29/04/2016 12:27:11因为我的时区很好'欧洲/巴黎'Normaly我应该有这个日期:29/04/2016 14:27:11

You can simply add 2 hours to the timestamp or you can create a DateTime object and modify it by adding 2 hours to it: 您只需将2小时添加到时间戳,或者您可以创建一个DateTime对象并通过向其添加2小时来修改它:

$timestamp = 1461857633 + 2 * 60 * 60;

or 要么

$dateTime = new DateTime();
$dateTime->setTimestamp(1461857633)->modify('+2 hours');
echo $dateTime->format('d/m/Y H:i:s');

or, another solution would be to calculate the seconds between the timezone you want to convert to and the Greenwich time zone (which is GMT+0) like: 或者,另一种解决方案是计算您要转换的时区与格林威治时区(GMT + 0)之间的秒数,如:

$greenwichTimeZone = new DateTimeZone('Greenwich Mean Time');
$parisTimeZone = new DateTimeZone('Europe/Paris');
$dateTimeGreenwich = new DateTime('now', $greenwichTimeZone);

$seconds = $parisTimeZone->getOffset($dateTimeGreenwich);
$dateTime = new DateTime();
$dateTime->setTimestamp(1461857633 + $seconds);
echo $dateTime->format('d/m/Y H:i:s');

$timestamp = 1461857633; $ timestamp = 1461857633;

$effectiveDate = strtotime("+120 minutes", $timestamp); $ effectiveDate = strtotime(“+ 120分钟”,$ timestamp);

$format = 'd/m/YH:i:s'; $ format ='d / m / YH:i:s'; $res = date($format, $effectiveDate); $ res = date($ format,$ effectiveDate);

echo $res; echo $ res;

date_default_timezone_set("UTC");
$HUTC = date("h");

date_default_timezone_set("Europe/Paris");
$HParis = date("h");

$diff = $HParis - $HUTC;

$timestamp = 1461857633;
$timestamp = 1461857633 + $diff * 60 * 60;
$format = 'd/m/Y H:i:s';
$res = date($format,$timestamp);
echo $res;

This will work both summer and winter time 这适用于夏季和冬季

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM