converting string to date in php not giving exact year

can somebody help me with my problem? I already figure out the problem but seems i do not now how to solve it without using explode(), so the problem is that the string date for example '02-15-17' i want to convert it in date() expected result (Feb 15, 2017) using strtotime() doesn't give me a right/exact year cause of the parameter 3 which is the year it has only 2 digit in HTML5 input tag date in chrome browser, can somebody help me to solve this problem? for the vision of my code here it is.

$user_date = '02-15-17';

echo date('M d, Y', strtotime($user_date));


Feb 2, 1917

2 answers

  • answered 2018-01-13 17:16 M0ns1f

    Since all your date are over 2000's use substr function try this

    $olddate = "02-15-17";
    $standarddate = substr($olddate,3,2) . "." . substr($olddate,0,2) . "." . "20" . substr($olddate,6,2);
    echo date("jS F, Y", strtotime($standarddate)).' - '.$standarddate;  

    output : 15th February, 2017 - 15.02.2017

  • answered 2018-01-13 17:18 The fourth bird

    You could also use a DateTime with the format function:

    $user_date = '02-15-17';
    $dateTime =DateTime::createFromFormat('m-d-y', $user_date);
    echo $dateTime->format('M d, Y');

    That will give you:

    Feb 15, 2017

    Output php