Search code examples
phpdatetimeformatdatetime

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


Solution

  • 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.