简体   繁体   中英

DateTime weekday issue in PHP

I am using the following:

$endtime = new DateTime(date('r', '1329717600'));
$endtime->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $endtime->format('w - l');

It should be outputting "1 - Monday"; but it is instead outputting "0 - Sunday"...

How do I fix this?

That's technically correct - the time/date on that timestamp would have been Sunday 10pm in LA, Monday 6am in UTC.

http://www.convert-unix-time.com/?t=1329717600

Try the following:

$endtime = new DateTime(date('r', '1329717600'), new DateTimeZone('America/Los_Angeles'));
echo $endtime->format('w - l');

PHP documentation comment on the setTimezone function:

The timestamp value represented by the DateTime object is not modified when you set the timezone using this method. Only the timezone, and thus the resulting display formatting, is affected.

$endtime = new DateTime;
$endtime->createFromFormat('U', 1329717600, new DateTimeZone('America/Los_Angeles'));
echo $endtime->format('w - l');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM