PHP Time mismatch between date () and time ()

I want to insert a user login log into a database table. In the column where I want to save the current time is "date_time decimal (10,0) NOT NULL DEFAULT" 0 "". When inserting data, I set the field as

$this->mytable->date_time = time(); 

My request was successful. But when I want to display the input time, it shows a time that does not match the time of my PC (local server). To display the time I'm writing

 echo date('Ymd h:i:s A', $log->date_time); 

I test several times, but it shows a time that is 4 hours less than the exact time. In my test, the current time is 2013-09-15 04:46:34 PM, but the row table shows 2013-09-15 12:46:34 PM.

Please help me. I can not find out about the error.

+4
source share
3 answers

You need to specify the time zone. The time() function will simply return a timestamp that is not time dependent.

When you use the date() function, you use the server time zone, I would recommend using a DateTime object:

 $timezone = new DateTimeZone("Etc/GMT-4"); $date = new DateTime("@".$log->date_time); // @-symbol indicates timestamp input $date->setTimezone($timezone); echo $date->format("r"); 

The following is a list of supported time zones http://php.net/manual/en/timezones.php

+5
source

Unfortunately. That was my fault. When inserting data, I set the time zone as

 if(function_exists('date_default_timezone_set')) date_default_timezone_set("Asia/Dhaka"); 

But when displaying the data, I forgot to set the time zone. It works great when I set the time zone, as I defined earlier on my display page. Thank you all for your help.

+3
source

try it

 <?php date_default_timezone_get(); echo time(); 

Manual

+2
source

Source: https://habr.com/ru/post/1502255/


All Articles