PHP:将本地时间转换为UTC [英] PHP: Convert Local Time to UTC

查看:500
本文介绍了PHP:将本地时间转换为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我得到一个像 2015/08/22 10:56 这样的字符串,并且此日期/时间字符串始终仅指一个特定的时区。我需要能够将其转换为以下格式:'Ymd\THis\Z',这是iCal格式。

Assume I get a string like 08/22/2015 10:56 PM and that this date/time string always refers to only one particular time zone. I need to be able to convert that to this format: 'Ymd\THis\Z', which is the iCal format.


  1. 如何将字符串转换为Zulu时间并转换为'Ymd\THis\Z'

  2. 然后我如何在该日期/时间之前添加30分钟?

曾经尝试入侵这与strtotime和DateTime一起使用,但是我担心我会以错误的方式进行操作。也许有一个更简单,更直接的解决方案?

Been trying to hack this with strtotime and DateTime, but I'm worried that I'm going about this the wrong way. Maybe there's a simpler and more straightforward solution?

推荐答案

您可以使用 DateTime ,例如以下示例:

You can use DateTime, example bellow:

<?php

$datetime = '08/22/2015 10:56 PM';
$tz_from = 'America/New_York';
$tz_to = 'UTC';
$format = 'Ymd\THis\Z';

$dt = new DateTime($datetime, new DateTimeZone($tz_from));
$dt->setTimeZone(new DateTimeZone($tz_to));
echo $dt->format($format) . "\n";

$minutes = 30;
$dt->add(new DateInterval('PT' . $minutes . 'M'));
echo $dt->format($format) . "\n";

这篇关于PHP:将本地时间转换为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆