使用 PHP 将数据库时间转换为 BST

发布于 2025-01-19 08:19:30 字数 422 浏览 0 评论 0原文

因此,我已经尝试了各种组合来从我的数据库(使用WordPress)中获得约会,以在英国夏季显示,我无法完成任何工作。

是否有任何简单的解决方案可以采用日期字符串,并确保在英国夏季距离UTC时间一个小时?

$classJson = $class->info;
$classJsonAsArray = json_decode($classJson, TRUE);

$classStartDate = strtotime($class->periodStart);
$classStartTime = date('H:i',$classStartDate);

因此,当前$ class-> ocerdstart返回:2022-04-06 08:30:00

该活动的时间应该是上午

9.30它显示为上午8.30。

So I've trying all sorts of combinations to get a date from my database (using Wordpress) to display in British Summer Time and I cannot get anything to work.

Is there any simple solution that can take the date string and make sure that in Summer Time in the UK it's an hour on from UTC time?

$classJson = $class->info;
$classJsonAsArray = json_decode($classJson, TRUE);

$classStartDate = strtotime($class->periodStart);
$classStartTime = date('H:i',$classStartDate);

So currently $class->periodStart returns: 2022-04-06 08:30:00

The time of that event should be 9.30am

All I need it to do is display the correct time, as at the moment, on the front end it displays as 8.30am.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2025-01-26 08:19:30

DateTime很好地处理时区。

$dateStringInUtc = '2022-04-06 08:30:00';
$date = new DateTime($dateStringInUtc, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/London'));

echo $date->format('Y-m-d H:i:s'); // will output 2022-04-06 09:30:00

DateTime handles timezones quite well.

$dateStringInUtc = '2022-04-06 08:30:00';
$date = new DateTime($dateStringInUtc, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/London'));

echo $date->format('Y-m-d H:i:s'); // will output 2022-04-06 09:30:00
万水千山粽是情ミ 2025-01-26 08:19:30

像接受的答案一样使用日期时间和时区是更好的方法。

但如果时区附加到日期字符串,它也适用于 strtotime。然后 Date 返回时间戳的本地时间。

$utcDate = '2022-04-06 08:30:00';
echo date('Y-m-d H:i:s',strtotime($utcDate.' UTC'));

Working with DateTime and timezones like in the accepted answer is the better way.

But it also works with strtotime if the timezone is appended to the date string. Date then returns the local time for a timestamp.

$utcDate = '2022-04-06 08:30:00';
echo date('Y-m-d H:i:s',strtotime($utcDate.' UTC'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文