0

I trying to get a date in the d/m/Y H:i format. Here's my code

$dateStr = "28/07/2016 10:00";
echo date('d/m/Y H:i', strtotime($dateStr));

Here's the ouput:

01/01/1970 01:00

I trying to convert to other formats, but the result still the same date. I know this kind of obvious but still can't understand why i'm getting that date as result.

André Freitas
  • 273
  • 1
  • 6
  • 17
  • why you don't want to use standart [`DateTime.createfromformat`](http://php.net/manual/ru/datetime.createfromformat.php) or [`strftime`](http://php.net/manual/ru/function.strftime.php)? – Arnial Jul 28 '16 at 12:06
  • 2
    Isn't your string already in that format? Also you don't need to convert yoru string twice into a timestamp. – Rizier123 Jul 28 '16 at 12:06
  • 1
    you did it so now what you want @abff – lazyCoder Jul 28 '16 at 12:07

3 Answers3

3

You can use DateTime class and it's createFromFormat method to parse the string to date in required format.

Like this,

$date = DateTime::createFromFormat('d/m/Y H:i', "28/07/2016 10:00");
$date = $date->format('Y-m-d H:i:s');
echo $date;
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
  • Why is there [date_create_from_format()](https://www.php.net/manual/en/datetime.createfromformat.php) considered procedural-style? – S. Dre Jun 09 '22 at 11:16
0

Use Following code

$dateStr = "28/07/2016 10:00";
$dateStr =str_replace("/","-",$dateStr);
echo date('d/m/Y H:i', strtotime($dateStr));
sunilwananje
  • 714
  • 5
  • 17
0

This code achieves what you want to do:

$dateString = '28/07/2016 10:00';
$date = \DateTime::createFromFormat('d/m/Y H:i', $dateString);
echo($date->format('d/m/Y H:i'));
Oliver
  • 353
  • 1
  • 2
  • 11