将时间转换为UTC

时间:2017-04-24 17:58:07

标签: php

我知道之前已经问过这个问题,但不知何故我无法在我的项目中使用它,所以请提示

我的php代码(如果还有其他用于检查是否设置了时间)

if (isset($_POST['startDate']) && isset($_POST['startTime']))
{
    $date = $_POST['startDate'] . " " . $_POST['startTime'];
    $date = new DateTime($date);
    $date->setTimezone(new DateTimeZone('UTC'));
//    $date = date('Y-m-d H:i:s', $date);

} else
{
    $date = date('Y-m-d H:i:s');
    $date = new DateTime($date);
    $date->setTimezone(new DateTimeZone('UTC'));
}

1 个答案:

答案 0 :(得分:1)

试一下::

<html>
<head></head>
<body>   

    <form action="#" method="POST">

        <input name="startDate" type="date"/>

      <br>

        <input name="startTime" type="time"/>

        <input name="Submit" type="submit" value="Submit" />
    </form>

</body>
</html>

<?php

if(isset($_POST['Submit'])){

if(isset($_POST['startDate']) && isset($_POST['startTime']))
{
    $date = $_POST['startDate'] . " " . $_POST['startTime'];
    // $date = "2016-02-01 00:00:01"; Like This..
    
    $date  = new DateTime($date, new DateTimeZone("Asia/Calcutta"));
    $date ->setTimezone(new DateTimeZone("UTC"));
  
  $output = $date ->format("Y-m-d H:i:s"); 
  //echo $output;

 $date = new DateTime($output); 

//    $date = date('Y-m-d H:i:s', $date);

} else
{
    $date ->format("Y-m-d H:i:s"); 
    $date = new DateTime($date, new DateTimeZone("Asia/Calcutta"));
    $date ->setTimezone(new DateTimeZone("UTC"));
}


}


?>