ProgrammerGirl
ProgrammerGirl

Reputation: 3223

DateTime class doing wrong conversion of Timestamp?

The following code is producing a wrong conversion of a Timestamp (1350553368):

$dateTime = new DateTime();
$dateTime->setTimeStamp(1350553368);    
echo $dateTime->format('F n, Y');

PHP converts it to October 10, 2012: http://codepad.viper-7.com/clum0f

However, that timestamp is actually for October 18, 2012: http://www.onlineconversion.com/unix_time.htm

I'm sure it's me, and not PHP, so what am I doing wrong? The code is pretty straightforward, so I can't figure it out.

Upvotes: 0

Views: 119

Answers (2)

Baba
Baba

Reputation: 95101

Form PHP DOC

  • n = Numeric representation of a month, without leading zeros
  • d = Day of the month, 2 digits with leading zeros

You should replace

  $dateTime->format('F n, Y');

With

  $dateTime->format('F d, Y');

Upvotes: 2

Cat
Cat

Reputation: 67502

You are using format 'F n, Y'. n is a numeric representation of the month (October is month 10). Use d (leading zeroes) or j (no leading zeroes). See PHP date() reference.

echo $dateTime->format('F d, Y');

Upvotes: 2

Related Questions