phpdateparsingdate-parsing

Parse m/d/Y formatted date string into $m, $d, and $y variables


If I've got a date string:

$date = "08/20/2009";

And I want to separate each part of the date:

$m = "08";
$d = "20";
$y = "2009";

How would I do so?

Is there a dedicated date function I should be using?


Solution

  • explode will do the trick for that:

    $pieces = explode("/", $date);
    $d = $pieces[1];
    $m = $pieces[0];
    $y = $pieces[2];
    

    Alternatively, you could do it in one line (see comments - thanks Lucky):

    list($m, $d, $y) = explode("/", $date);