0

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.

<?php
$user_date = '02-15-17';

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

result:

Feb 2, 1917
kim-dev
  • 17
  • 6
  • Use DateTime objects, and tell it what format to expect: `$dto = DateTime::createFromFormat('m-d-y', $user_date); echo $dto->format('Y-m-d');` – Mark Baker Jan 13 '18 at 17:17

2 Answers2

1

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

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

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

M0ns1f
  • 2,705
  • 3
  • 15
  • 25