0

I keep getting an error when I try to add a blog entry into my database. I have a simple syntax highlighter but it is not showing where something is not right.

Error: PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' on line 75 and 71

My script:

if(!isset($error)){

try {

    //insert into database
    $stmt = $db->prepare('INSERT INTO blog_posts (postTitle,postDesc,postCont,postDate) VALUES (:postTitle, :postDesc, :postCont, :postDate)') ; //line 71
    $stmt->execute(array(
        ':postTitle' => $postTitle,
        ':postDesc' => $postDesc,
        ':postCont' => $postCont,
        ':postDate' => (new DateTime())->format('Y-m-d H:i:s') //Line 75
    ));

    //redirect to index page
    header('Location: index.php?action=posted&title='.$postTitle.'');
    exit;

} catch(PDOException $e) {
    echo $e->getMessage();
}

}
4
  • 1
    (new DateTime())->format('Y-m-d H:i:s') <--- where did you see such a syntax? Commented Apr 10, 2014 at 3:10
  • possible duplicate of PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR Commented Apr 10, 2014 at 3:17
  • @zerkms this syntax exists in Laravel, probably the OP was trying to apply the framework logic into plain php. Commented Apr 10, 2014 at 3:29
  • @zerkms Hi again, in the following link you can find a usage example of the type (new Model)->method(), github.com/laravel/framework/issues/1436 , look for the first comment from May 29. Commented Jun 28, 2014 at 20:43

2 Answers 2

1

your usage of format() is wrong, change:

...
':postDate' => (new DateTime())->format('Y-m-d H:i:s') 

to

...
$date = new DateTime();
$formattedDate = $date->format('Y-m-d H:i:s');
....
':postDate' => $formattedDate
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was my problem. I see why that is wrong.
0

This works for me:

$today = (new \DateTime('NOW'))->format('d-m-y');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.