将DateTime连接到PHP中的字符串变量时出错

I'm trying to create a string that includes the current server time but I'm getting an error when performing the concatenation operation.

The following works just fine:

$date = new DateTime('now', new DateTimezone('Africa/Johannesburg'));
echo $date->format('Y-m-d H:i:s');

$message = 'There is a new event occurred ';
echo $message;

but when I try to concatenate the message and the formatted date together, I get an HTTP 500 error.

$date = new DateTime('now', new DateTimezone('Africa/Johannesburg'));
echo $date->format('Y-m-d H:i:s');

$message = 'There is a new event occurred at '.date->format('Y-m-d H:i:s');  
echo $message;

What am I not understanding?

As you've figured out, the second example should have been:

$message = 'There is a new event occurred at '. $date->format('Y-m-d H:i:s');  

Based on your statement that you were struggling for two hours, you probably had just a blank screen or an "Internal Server Error" type of page, which didn't give you any details.

To debug that kind of problem:

1) Find the error log for your web server (e.g. error_log). You would find something in there like this:

[Thu Mar 31 19:28:24 2016] [error] [client 10.42.88.12] PHP Parse error:
syntax error, unexpected T_OBJECT_OPERATOR in /var/www/htdocs/datetest.php on line 9

2) Find the php.ini file that controls your PHP installation and set the value:

display_errors = On 

That will display the same "Parse error:" directly in your web browser. You should not have that setting on for your production server, but it's very helpful during development.