Yeti
Yeti

Reputation: 5818

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?

Upvotes: 15

Views: 127319

Answers (7)

Sani Kamal
Sani Kamal

Reputation: 1238

Its just spelling mistake it should be like this

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

Upvotes: 2

Ravi
Ravi

Reputation: 1

<?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();
                    }
        ?>

Upvotes: 0

Kalaivani M
Kalaivani M

Reputation: 1290

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

Upvotes: 5

aman tilak
aman tilak

Reputation: 433

Its just spelling mistake it should be like this

date_default_timezone_set('Asia/Kolkata');

Upvotes: 30

RaJ
RaJ

Reputation: 1

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

Upvotes: 0

Daksh B
Daksh B

Reputation: 262

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.

Upvotes: 2

DAn
DAn

Reputation: 74

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?

Upvotes: 5

Related Questions