简体   繁体   中英

date_default_timezone_set showing incorrect time

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.

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

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

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

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?

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.

date_default_timezone_set('Asia/Kolkata');

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.

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.

Its just spelling mistake it should be like this

date_default_timezone_set('Asia/Kolkata');
$timestamp = date("Y-m-d H:i:s");
$ab = date_default_timezone_get(); 
date_default_timezone_set($ab); 
echo date('dd-mm-Y H:i:s');
<?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();
                    }
        ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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