Convert a unix timestamp to a human readable date in PHP?

南楼画角 提交于 2019-12-01 14:57:49

问题


I have a timestamp value from PHP: 1188604800000

When I format the time to human readable like this:

date("m/d/Y", 1188604800000)

It prints:

05/21/39635

If I put the number into an online Unix Timestamp converter I get:

Sat, 01 Sep 2007 00:00:00 GMT

What am I doing wrong?


回答1:


PHP uses seconds-based timestamps, so divide 1188604800 by 1000 and you are good.

php> echo date('Y-m-d', 1188604800000/1000);
2007-09-01



回答2:


I was having trouble with my date being one day off and I had to manually set the default timezone to match my location by using

<?php date_default_timezone_set("Australia/Perth"); ?>

A list of support timezones can be found here - http://www.php.net/manual/en/timezones.php

(I don't have enough rep to comment so can someone merge that with the actual answer?)



来源:https://stackoverflow.com/questions/10233829/convert-a-unix-timestamp-to-a-human-readable-date-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!