R
R
Roman Yakushev2015-01-11 02:14:46
PHP
Roman Yakushev, 2015-01-11 02:14:46

Php new DateTime() changes the time from 10 o'clock to 22 o'clock. Why?

Problem: php itself changes 10 hours to 22 hours.
Execution order:
php is executed. At the very beginning, I set the time to international in it: Then I write the current date and time to the database like this:
date_default_timezone_set('UTC');

$tmptime = new DateTime();
$tmpstr = "INSERT INTO ... email_send='".$tmptime->format(DATETIMEFORMAT)."'";

The email_send column in MYSQLI is just a "datetime" type column with no length specified.
After some time, I pull this time from the database and try to compare it with the current time:
$email_send = $data['email_send'];
$alerts->add('$email_send='.$email_send,'info',$email_send);
$datetime1 = DateTime::createFromFormat(DATETIMEFORMAT, $email_send); //тут пробовал по разному - результат один и тот же.
$datetime2 = new DateTime();
$alerts->add('$datetime1='.$datetime1->format(DATETIMEFORMAT),'info',$datetime1);
$alerts->add('$datetime2='.$datetime2->format(DATETIMEFORMAT),'info',$datetime2);
$interval = $datetime1->diff($datetime2);
$alerts->add('$interval='.$interval->format('%y-%m-%d %h:%i:%s'));

As a result, I get:

$email_send=2015-01-10 10:44:47 = 2015-01-10 10:44:47 // string received from the database
$datetime1=2015-01-10 10:44:47 = DateTime Object ( [date] => 2015-01-10 10:44:47 [timezone_type] => 3 [timezone] => UTC )
$datetime2=2015-01-10 10:54:39 ​​= DateTime Object ( [date] => 2015-01 -10 22:54:39 ​​[timezone_type] => 3 [timezone] => UTC )
$interval=0-0-0 12:9:52

At first glance, everything is OK - the difference should be almost 10 minutes. But wait, where... where did 12 o'clock come from? and from there - that when viewing print_r - the current date ($datetime2) becomes already after lunch. inexplicably.
Attention to the question - why does php change the time to 22 hours? under exactly the same conditions.
A crutch like "add 12 hours for all cases" does not suit me. I want to get to the bottom of the truth.
Why do I need to generate time in php? Why not use mysql's built-in timestamp? I need to unify time everywhere in UTC. So that later I could convert the time for each specific visitor to his local time. And the time configured on the php and mysql server may differ. Depends on the curvature of the hosting.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2015-01-11
@CanVas

the default date and time format is taken from php.ini (sort of...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question