Reputation: 2783
$date = new DateTime(2011-10-05);
echo $date->format('Y-m-d H:i:s');
Running the above code, the page is displayed nothing. Just wondering is there anything wrong on the code and I used the PHP5 (Version 5.3.0).
Can anyone help me? Thanks in advance!
--Update--- After adding quotes, still nothing is displayed.
$date = new DateTime("2011-10-05");
echo $date->format('Y-m-d H:i:s');
Upvotes: 1
Views: 478
Reputation: 693
Quotes weren't my fix for the "displayed nothing" problem, either (PHP 5.3.10 on Win 7 Enterprise, 64-bit). @gnud's suggestion pointed me to my problem:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.
New code:
$timezone = new DateTimeZone('America/Chicago');
$dateNow = new DateTime('now', $timezone);
echo $dateNow->format('Y-m-d H:i:s');
Gives the expected result:
2012-09-04 17:12:28
Upvotes: 1
Reputation: 18672
You forgot to make the argument you're passing to the DateTime
constructor a string. This fixes it:
$date = new DateTime('2011-10-05');
Upvotes: 1
Reputation: 47776
Well, your code works fine but you did not put the date in quotes, and thus PHP considers it as the formula
2011 - 10 - 05
Which gives 1996. You'll want to give it a string instead. Either way, it works fine.
Upvotes: 1
Reputation: 71908
Use quotes around the date when you create the object:
$date = new DateTime("2011-10-05");
Upvotes: 1
Reputation: 318478
new DateTime(2011-10-05)
is equal to new DateTime(1996)
and thus makes no sense. You probably meant new DateTime('2011-10-05')
Upvotes: 2