14

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?

5
  • 2
    Are you sure this is not simply the clock of the server that is not running correctly? THe Indian Time Zone (IST) is 5:30 off from UTC en.wikipedia.org/wiki/UTC%2B05:30
    – Pekka
    Oct 1, 2010 at 18:06
  • Just checked and looks like the server clock is indeed incorrect!
    – Yeti
    Oct 1, 2010 at 18:16
  • @Yeti, did you find a solution to the problem?, Right now my script says its 8:16pm where as its actually 7:46pm here in India. I am on xampp.
    – Daksh B
    Aug 11, 2015 at 14:17
  • @DakshB Just checked and the problem seems to have solved automatically. So probably the time was incorrectly set on the server.
    – Yeti
    Aug 12, 2015 at 14:51
  • There is a very different result of using this with datetime column versus the timespan column in mysql. Something to keep in mind while looking at answers to this question.
    – ambassallo
    Dec 14, 2019 at 19:22

7 Answers 7

29

Its just spelling mistake it should be like this

date_default_timezone_set('Asia/Kolkata');
5

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?

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

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.

2

Its just spelling mistake it should be like this

date_default_timezone_set('Asia/Kolkata');
$timestamp = date("Y-m-d H:i:s");
0
$ab = date_default_timezone_get(); 
date_default_timezone_set($ab); 
echo date('dd-mm-Y H:i:s');
2
  • please provide some context as to why you think this is the answer
    – DForck42
    Nov 7, 2016 at 17:39
  • echo date('dd-mm-Y H:i:s'); will print date and month twice, how come this can be answer to above question!
    – sabin
    Jun 12, 2017 at 8:40
0
<?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();
                    }
        ?>
1
  • Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: How do I write a good answer? Jun 18, 2018 at 9:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.