phpstringdatetext-extractiondate-parsing

Parse a Y-m-d formatted date string into $year, $month, and $day variables


I have a string of 2011-03-06.

How do I extract the hyphen-separated values into individual variables:

$day = "06";
$month = "03";
$year = "2011";

Solution

  • You can use explode() to separate the elements, and list() to assign them to three separate variables.

    list($year, $month, $day) = explode('-', $date);