Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime: Syntax error, unexpected T_OBJECT_OPERATOR

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) {

1 Answers

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) { 
like image 178
John Conde Avatar answered Feb 07 '26 14:02

John Conde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!