John Higgins John Higgins - 6 months ago 38
PHP Question

PHP - date formatting issue

I am trying to format dates from a UK format to YYYY-MM-DD an am getting weird results.

The code I am using is:

echo "<br>".$_GET['trans_date'];
echo "<br>".$_GET['next_payment'];
echo "<br>".$_GET['payment_date'];
echo "<br><br>".date("Y-m-d", strtotime($_GET['trans_date']));
echo "<br>".date("Y-m-d", strtotime($_GET['next_payment']));
echo "<br>".date("Y-m-d", strtotime($_GET['payment_date']));


And I get the following results:

19/05/2016
01/06/2016
19/05/2016

1970-01-01
2016-01-06
1970-01-01


I am expecting the following results:

19/05/2016
01/06/2016
19/05/2016

2016-05-19
2016-01-01
2016-05-19


Can anyone what's going wrong?

Thanks,

John

Answer

This should work :

echo "<br><br>".date("Y-m-d", strtotime(str_replace('/', '-', $_GET['trans_date'])));
echo "<br>".date("Y-m-d", strtotime(str_replace('/', '-', $_GET['next_payment'])));
echo "<br>".date("Y-m-d", strtotime(str_replace('/', '-', $_GET['payment_date'])));
Comments