Storm Parker
Storm Parker

Reputation: 633

PHP convert date to string

I have a date object that I would like to convert to String. I tried looking it up but haven't came up with a good answer. The date is retrieved from a db and it looks like this:

object(DateTime)#1433 (3) {
    ["date"]=> string(19) "2014-06-04 00:00:00"
    ["timezone_type"]=> int(3)
    ["timezone"]=> string(19) "America/Los_Angeles"
}

I need it to just be "2014-06-04" to set as a parameter in another function. How do I convert it to just a string?

Upvotes: 0

Views: 15882

Answers (2)

S.Pols
S.Pols

Reputation: 3434

You can use the format function:

$dateString = $yourDateObject->format('Y-m-d');

Upvotes: 5

Gergo Erdosi
Gergo Erdosi

Reputation: 42028

You already have a DateTime object, so you can use the format() method to format the date:

$date->format('Y-m-d');

You can find the list of format characters (like Y, m, or d) in the manual of date().

Upvotes: 2

Related Questions