Jason Axelrod
Jason Axelrod

Reputation: 7815

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?

Upvotes: 1

Views: 1151

Answers (3)

Vytautas
Vytautas

Reputation: 3539

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

Upvotes: 0

Sascha Galley
Sascha Galley

Reputation: 16101

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.

Upvotes: 3

BenOfTheNorth
BenOfTheNorth

Reputation: 2872

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

Upvotes: 3

Related Questions