0

I have an array of date strings like this in php.

28.09.18 13:42
28.09.18 14:12
28.09.18 21:18
28.09.18 21:23
28.09.18 21:28
10.04.19 23:36

My goal is to change the format to something like Y-m-d H:i:s. So I try this code.

foreach($dates as $date) {
    echo date('Y-m-d', strtotime($date)).PHP_EOL;
}

The output looks like this:

2018-09-28
2018-09-28
2018-09-28
2018-09-28
2018-09-28
1970-01-01

As you can see the last date is wrong. Why it's not parsed probably? Is there any solution for this?

Markus
  • 1,909
  • 4
  • 26
  • 54
  • Use the second answer. – AbraCadaver Oct 10 '19 at 19:34
  • 1
    I think `strtotime` assumes `4/10/2018` (April) and `10-4-2018` (April) but using dots is ambiguous as to whether it is April or October. `28.09.18` is not ambiguous as there is no 28th month but there is a 9th. – AbraCadaver Oct 10 '19 at 19:37
  • Now I use DateTime::createFromFormat('d.m.y h:i', $date). But it always returns false – Markus Oct 10 '19 at 19:43
  • `h` is only 12 hour time use `H` for 24. Also format it `DateTime::createFromFormat('d.m.y H:i', $date)->format('Y/m/d')`. This is your friend `print_r(DateTime::getLastErrors());`. – AbraCadaver Oct 10 '19 at 19:58

0 Answers0