admin管理员组文章数量:1436133
My situation is that get always the dates one day behind.
For example, I have this value -243219600
which is relative to date 18/04/1962
After a
date('d/m/Y', -243219600);
Output is :
17/04/1962
-243219600 seconds from January 1 1970 00:00:00 UTC in javascript is here you get correct Date.
My situation is that get always the dates one day behind.
For example, I have this value -243219600
which is relative to date 18/04/1962
After a
date('d/m/Y', -243219600);
Output is :
17/04/1962
-243219600 seconds from January 1 1970 00:00:00 UTC in javascript is here you get correct Date.
Share Improve this question asked Sep 10, 2015 at 11:17 AlaeddineAlaeddine 1,6972 gold badges26 silver badges48 bronze badges 3-
I get proper
18/04/1962
as output when I try your code. – jitendrapurohit Commented Sep 10, 2015 at 11:26 -
I get the 17th - I'm using UTC. Specifically with
$dt = new DateTime(); $dt->setTimestamp(-243219600); echo $dt->format('r');
I getTue, 17 Apr 1962 23:00:00
. So, pretty close to the 18th, but not quite. – Darragh Enright Commented Sep 10, 2015 at 11:37 - @JitendraPurohit can you tell me what Time zone you are using? – Alaeddine Commented Sep 10, 2015 at 12:55
2 Answers
Reset to default 5The output of date()
depends on the configured time zone. If you add the time and timezone, you can see it. In my case it is CET:
echo date('d/m/Y H:m:i T', -243219600);
//prints: 18/04/1962 00:04:00 CET
Solution with date()
If you want date()
to use UTC
, use date_default_timezone_set
:
date_default_timezone_set('UTC');
echo date('d/m/Y H:m:i T', -243219600);"
Output
17/04/1962 23:04:00 UTC
(you see, since it is one hour before midnight in UTC, the date depends on the timezone)
Solution with DateTime
:
The DateTime
class uses always UTC if it is constructed by a Unix timestamp:
From the documentation:
Note:
The
$timezone
parameter and the current timezone are ignored when the$time
parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
So you can use the following code as well:
echo (new DateTime('@-243219600'))->format('d/m/Y');
Check the timezone of your PHP, set it so it's the same as your puter (since your using javascript).
本文标签: javascriptDate function in PHP gives always date one day behindStack Overflow
版权声明:本文标题:javascript - Date function in PHP gives always date one day behind - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745378618a2656054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论