2

i am having troubles with datetime attribute in my class.

Next code is in my twig template.

<div class="process-photo"><img src="{{ photo.getPhotoUrl }}" /></div>

This is the getPhotoUrl method

 public function getPhotoUrl()
    {
        return '/web/uploads/photos/'.$this->getUserId().'/'.$this->getPhotoUploadDate().'/'.$this->getName();
    }

This is the getPhotoUploadDate method

public function getPhotoUploadDate() {
    return date('Y-m-d', strtotime($this->creationDate));
    }

I am getting next error - Warning: strtotime() expects parameter 1 to be string, object given

If i try this way

public function getPhotoUrl()
    {
        return '/web/uploads/photos/'.$this->getUserId().'/'.$this->creationDate.'/'.$this->getName();
    }

I am getting next error - Catchable Fatal Error: Object of class DateTime could not be converted to string

what i am doing wrong??

3
  • 3
    The error message Object of class DateTime could not be converted to string is all the information you need. $this->creationDate is an DateTime object, so instead of date('Y-m-d', strtotime($this->creationDate)) you could easily $this->creationDate->format('Y-m-d'). Why would you create a date from a date you already have? Commented Oct 29, 2015 at 12:25
  • yes that worked! thanks! Commented Oct 29, 2015 at 12:28
  • 1
    Glad I could help, I answered your question, you can accept it if my solution worked. Commented Oct 29, 2015 at 12:37

1 Answer 1

4

The error message

Object of class DateTime could not be converted to string

is all the information you need.

$this->creationDate is an DateTime object, it already contains the Date information.

So instead of date('Y-m-d', strtotime($this->creationDate)) you could easily $this->creationDate->format('Y-m-d').

You're trying to create a new Datestring from another datestring and instead of giving a datestring you're giving the DateTime Object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.