doqs8936 2017-06-13 17:39
浏览 133
已采纳

在PHP中使用strtotime()和在MySQL中使用UNIX_TIMESTAMP()时的不同时间戳

I have a date stored in the database. The date is 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 output it like date('d.m.Y', $mydate)

I get 28.02.2017 as a result.

Is this a "February-Problem"? How can I make date() do it right?

----------------- Edit 1 -----------------

I put this in my code already before.

# set timezone
date_default_timezone_set('Europe/Vienna');

# daylight-saving time
if(date('I') < 1){
 mysqli_query($db, "SET time_zone = '+01:00'");
}else{
 mysqli_query($db, "SET time_zone = '+02:00'");
}

----------------- Edit 2 -----------------

Ok, I generated 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

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

Current Time is the same Time displayed on my Computer. So it is the correct Time and the Time-Settings seem to be ok. "Current Time" is generated by the date()-Function in PHP and with MySQLs NOW().

Col 1 is the UNIX_TIMESTAMP of the MySQL-Query.

Col 2 is the Date generated with the PHP-Date-Function and Col 1.

Col 3 is the Unix Timestamp of strtotime().

Col 4 is the Date generated with the PHP-Date-Function and Col 3.

Col 5 is the Date formated with DATE_FORMAT(xdate, '%Y-%m-%d').

Col 6 is the Date directly from the database.

As you can see, the first eight Rows are wrong calculated by the date()-Function (2nd Column) which is fed with the (wrong?) UNIX_TIMESTAMP() of the MySQL-Query:

date('d.m.Y', $mydate)

I tested what happens, if I replace the line

 mysqli_query($db, "SET time_zone = '+02:00'");

with

 mysqli_query($db, "SET time_zone = '+01:00'");

The date-Function gives back the correct Date, BUT the NOW() in MySQL deliveres the wrong Time then.

When I remove the part with the Settings from the Script (see Edit 1), everything is equal, but then I have the wrong Timezone.

Has anyone a clue for me?

  • 写回答

3条回答 默认 最新

  • douyou8266 2017-06-16 12:24
    关注

    Ok, my Settings seem to be okay and it seems to be, that strtotime() in PHP works with Timezones in opposition to UNIX_TIMESTAMP in MySQL. I decided to replace the SELECTS which select UNIX_TIMESTAMP()s and convert it to a Timestamp with strtotime(). Now it works as it shall work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?