php date_default_timezone_set 显示不正确的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3841852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:17:29  来源:igfitidea点击:

date_default_timezone_set showing incorrect time

php

提问by Yeti

I uploaded the following to a server in the US:

我将以下内容上传到美国的服务器:

date_default_timezone_set('Asia/Calcutta'); 
echo date("Y-m-d H:i:s"); // time in India

The time displayed is 15 minutes prior to that of the actual time in India.

显示时间比印度实际时间早15分钟。

What am I doing wrong here? What code will alwaysshow the time in India accurate to the second?

我在这里做错了什么?什么代码将始终显示印度的时间精确到秒?

采纳答案by DAn

It is all relative to the time set on the server. Check the correct time is actually set on the server - maybe it's 15minutes out for the timezone it is set in?

这一切都与服务器上设置的时间有关。检查服务器上实际设置的正确时间 - 也许它设置的时区是 15 分钟?

回答by aman tilak

Its just spelling mistake it should be like this

只是拼写错误应该是这样的

date_default_timezone_set('Asia/Kolkata');

回答by Kalaivani M

date_default_timezone_set('Asia/Kolkata');
$timestamp = date("Y-m-d H:i:s");

回答by Daksh B

I had the same problem when testing out the code on my local xampp server. Here is what I did, changed the timezone default setting to in the php.ini file.

我在本地 xampp 服务器上测试代码时遇到了同样的问题。这是我所做的,将时区默认设置更改为 php.ini 文件中。

date_default_timezone_set('Asia/Kolkata');

date_default_timezone_set('亚洲/加尔各答');

yes agree, not entirely necessary

是的 同意,并非完全必要

Then, declared date_default_timezone_set('Asia/Kolkata') on my config.php file, entirely necessary to set the timezone to your local timezone.

然后,在我的 config.php 文件中声明 date_default_timezone_set('Asia/Kolkata') ,这完全有必要将时区设置为您的本地时区。

Even after this my time and date were 12 hours apart and here is the catch, on checking my windows operating system settings I found that my system time was wrongly set. On correcting my (windows) operating system time zone everything worked perfectly.

即使在此之后,我的时间和日期相隔 12 小时,这就是问题所在,在检查我的 Windows 操作系统设置时,我发现我的系统时间设置错误。在更正我的(Windows)操作系统时区时,一切正常。

回答by Sani Kamal

Its just spelling mistake it should be like this

只是拼写错误应该是这样的

date_default_timezone_set('Asia/Kolkata');
$timestamp = date("Y-m-d H:i:s");

回答by Ravi

<?php
                class datashow extends connection{
                    function __construct(){}
                    function showData($id){
                    $iddate=$_SESSION["datesession"];   
                    $qry = "SELECT * FROM data where EnNo='$id' group by Date order by Date DESC";
                    $qry1 = "SELECT * FROM data where EnNo='$id' order by Date DESC";
                    $row=mysqli_query($this->conn, $qry) or die ("query Failed...!");
                    $row1=mysqli_query($this->conn, $qry1) or die ("query Failed...!");
                        while($rec=mysqli_fetch_array($row)){
                            echo "<tr>";
                            echo "<td class='text-center'>".$rec['Date']."</td>";
                            while($rec1=mysqli_fetch_array($row1)){
                                $time1=$rec1["Time"];
                                $time = explode(':', $time1);
                                if($time[0] <= 12){
                                    echo "<td class='text-center' id='timeIn'>".$time[0].":".$time[1].":".$time[2]."</td>";
                                }
                                else if ($time[0] >= 12){
                                    echo "<td class='text-center' id='timeOut'>"."-"."</td>";
                                    echo "<td class='text-center' id='timeOut'>".$time[0].":".$time[1].":".$time[2]."</td>";
                                }
                                }
                                echo "</tr>";   
                            }
                        }
                    function __destruct(){}
                    }
                    if (isset($_REQUEST["btnsub"])){
                    $objcon = new datashow;
                    $objcon->setconnection();
                    $objcon->showData($_REQUEST["btnsub"]);
                    $objcon->CloseCon();
                    }
        ?>

回答by RaJ

$ab = date_default_timezone_get(); 
date_default_timezone_set($ab); 
echo date('dd-mm-Y H:i:s');