I have a date stored in a database. Date - 2017-03-01. The field is a date field.
When i use
DATE_FORMAT(orderdate, '%d.%m.%Y') as mydate
in my MySQL Query, "01.03.2017" is displayed.
When i use
UNIX_TIMESTAMP(orderdate) as mydate
and print it as date('dmY', $mydate)
As a result, I get 02/28/2017.
Is this a "February issue"? How can I do date () do it right?
----------------- Edit 1 -----------------
I have put this in my code before.
# set timezone date_default_timezone_set('Europe/Vienna');
----------------- Edit 2 -----------------
Ok, I created a MySQL table with this content (Field-Type: "date"):
xdate 2017-01-01 2017-01-15 2017-01-31 2017-02-01 2017-02-15 2017-02-28 2017-03-01 2017-03-15 2017-03-31 2017-04-01 2017-04-15 2017-04-30 2017-05-01 2017-05-15 2017-05-31 2017-06-01 2017-06-15
The generated output from my Script:
Current Time 2017-06-16 02:31:08 PHP-Time 2017-06-16 02:31:08 MySQL-Time Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 1483221600 2016-12-31 1483225200 2017-01-01 2017-01-01 2017-01-01 1484431200 2017-01-14 1484434800 2017-01-15 2017-01-15 2017-01-15 1485813600 2017-01-30 1485817200 2017-01-31 2017-01-31 2017-01-31 1485900000 2017-01-31 1485903600 2017-02-01 2017-02-01 2017-02-01 1487109600 2017-02-14 1487113200 2017-02-15 2017-02-15 2017-02-15 1488232800 2017-02-27 1488236400 2017-02-28 2017-02-28 2017-02-28 1488319200 2017-02-28 1488322800 2017-03-01 2017-03-01 2017-03-01 1489528800 2017-03-14 1489532400 2017-03-15 2017-03-15 2017-03-15 1490911200 2017-03-31 1490911200 2017-03-31 2017-03-31 2017-03-31 1490997600 2017-04-01 1490997600 2017-04-01 2017-04-01 2017-04-01 1492207200 2017-04-15 1492207200 2017-04-15 2017-04-15 2017-04-15 1493503200 2017-04-30 1493503200 2017-04-30 2017-04-30 2017-04-30 1493589600 2017-05-01 1493589600 2017-05-01 2017-05-01 2017-05-01 1494799200 2017-05-15 1494799200 2017-05-15 2017-05-15 2017-05-15 1496181600 2017-05-31 1496181600 2017-05-31 2017-05-31 2017-05-31 1496268000 2017-06-01 1496268000 2017-06-01 2017-06-01 2017-06-01 1497477600 2017-06-15 1497477600 2017-06-15 2017-06-15 2017-06-15
The current time is the same time displayed on my computer. So this is the right time, and the time settings look fine. The "current time" is generated by the date () function - in PHP and with MySQL NOW ().
Col 1 is UNIX_TIMESTAMP MySQL-Query.
Col 2 is a date generated using the PHP-Date-Function and Col 1.
Col 3 is the Unix timestamp for strtotime ().
Col 4 is a date generated using the PHP-Date-Function and Col 3.
Col 5 is a date generated using DATE_FORMAT (xdate, '% Y-% m-% d').
Col 6 is a date directly from the database.
As you can see, the first eight rows are incorrect, calculated using the function date () - Function (2nd column), which is loaded using (incorrect?) UNIX_TIMESTAMP () MySQL-Query:
date('dmY', $mydate)
I checked what happens if I replace the string
mysqli_query($db, "SET time_zone = '+02:00'");
from
mysqli_query($db, "SET time_zone = '+01:00'");
The Date function returns the correct date, BUT NOW () in MySQL provides the wrong time.
When I delete the settings part from the Script (see Edit 1), it’s all the same, but then I have the wrong time zone.
Will someone tell me?