DateTime: Syntax error, unexpected T_OBJECT_OPERATOR

date, datetime, php, syntax-error

Solution

Class member access on instantiation was added in PHP 5.4. You are probably running PHP 5.3 so you cannot use that syntax.

Change:

if ((new DateTime($date))->diff(new DateTime())->days > 10) { 

to:

$date = new DateTime($date);
if ($date->diff(new DateTime())->days > 10) { 

Problem

It works fine on `localhost`, but when I upload it to my host online it shows this error: ``` syntax error, unexpected T_OBJECT_OPERATOR ``` How can I fix it? Do I have to define the `DateTime` function somewhere? ``` if ((new DateTime($date))->diff(new DateTime())->days > 10) { echo 'test'; } ``` UPDATE: ``` $date = DateTime::createFromFormat('y-M-d l H:i a', $date); if ($date->diff(new DateTime())->days > 10) { ```

Original source