nasri_thamer
nasri_thamer

Reputation: 109

Format a date in PHP

How can i use the format methode in the Class DateTime in PHP and take some format like :

'Y-m-d H:i' or just the date 'Y-m-d'.

this my code and i putted a picture what i take when i do the dump :

    $thisday = new \DateTime(); // I want to take the system date is it ok this instruction ?

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

    dump($thisday);
    die();

How can i take just the 2016-04-21 ?

Or take the date, hour and minutes ?

enter image description here

Upvotes: 0

Views: 223

Answers (2)

Jacob See
Jacob See

Reputation: 775

format() does not transform the original DateTime object into your formatted result, it simply returns your formatted result, which you are not assigning to anything. You need to assign

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

to a new variable, and use that.

Upvotes: 4

Christian Gollhardt
Christian Gollhardt

Reputation: 17004

You need to assign the result a variable. DateTime::format is returning the value, not changing the object itself

$str = $thisday->format('Y-m-d');
dump($str);

Upvotes: 1

Related Questions