5

Here is my code:

$timezone = new \DateTimeZone('America/New_York');
    $date1 = new \DateTime (date("Y:m:d H:i:s", time()), $timezone);
    $date1->add(new \DateInterval ("PT24H"));
    echo $date1->format('Y-m-d h:s:m');

This should add 24 hours to the current time. Problem is, each time I refresh the page, I get a different result. And I don't mean by just a couple seconds like one would expect, I mean lots of minutes. Some above the actual time some below it.

Three refreshes right now at 9:51pm give the following results: 2013-03-26 09:09:03, 2013-03-26 09:17:03, 2013-03-26 09:30:03

Why can't I get the real time? What is going on?

Thanks for the help!

tttony
  • 4,944
  • 4
  • 26
  • 41
user1513171
  • 1,912
  • 6
  • 32
  • 54

1 Answers1

9

The error is in your last echo statement

echo $date1->format('Y-m-d h:s:m');

change this to:

echo $date1->format('Y-m-d h:i:s');

Every refresh is refreshing the middle value which you had set to seconds rather than minutes therefore every 3 second update for example was showing to you as a 3 minute update. In addition you had your minute showing as m which is the date format for month number so it was showing as 3 for the numerical representation of March. I have changed this to i to represent minutes.

I got your code above working here in London with the following code:

$date1 = new DateTime('America/New_York');
$date1->add(new DateInterval("PT24H"));
echo $date1->format('Y-m-d h:i:s');

This now shows 10:07 PM March 26th which is 24 hours after the current time in New York

Peter Featherstone
  • 7,835
  • 4
  • 32
  • 64
  • Well that's embarrassing. But even doing that, it's off by almost and hour. Daylight savings time perhaps? It's 9:46 and it's currently showing 9:03 on a refresh. – user1513171 Mar 26 '13 at 01:57
  • I have updated the answer above to include the correct code. Try it and let me know if it works for you too. – Peter Featherstone Mar 26 '13 at 02:14