2

I have a date value in the format of Day MM dd, yyyy (e.g. Wed Aug 17, 2016) in PHP and I want to convert it to the format yyyy-mm-dd (2016-08-dd).

I have tried following:

$journeyDate = 'Wed Aug 17, 2016';
$time =strtotime(stripslashes($journeyDate));
echo date("Y-m-d", strtotime($time));

But the above code converts the date to desired format but returns date as 1970-01-01 which is wrong. What can I do to fix this problem?

Tim Visée
  • 2,988
  • 4
  • 45
  • 55
NikMish
  • 109
  • 1
  • 3
  • 12

2 Answers2

0

You're applying strtotime twice - once when initializing $time and once again when applying date to it. Remove one of them and you should be OK:

$journeyDate = 'Wed Aug 17, 2016';
$time = strtotime(stripslashes($journeyDate));
echo date("Y-m-d", $time);
# Here ------------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Use theDateTime Class like so:

    <?php

        $journeyDate    = 'Wed Aug 17, 2016';
        $jdObjDT        = new DateTime($journeyDate);

        var_dump($jdObjDT->format("Y-m-d"));
        // PRODUCES::: string '2016-08-17' (length=10)
Poiz
  • 7,611
  • 2
  • 15
  • 17