0

I have date from monthpicker in this format: 2014 April , and i want to change it to 2014-04-01 before inserting it to mysql. I'm trying to use strtotime:

$b = date("Y-m-d", strtotime($_POST['month']));
echo $b;

Result is: 1970-01-01. I dont get it.

  • 1
    possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Amal Murali Apr 06 '14 at 17:22

5 Answers5

1

Use DateTime

$date = new DateTime("2014 April");
echo $date->format("Y-m-d");
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
1
$date = DateTime::createFromFormat('Y F', '2014 April');
echo $date->format('Y-m-01');
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

Chances are month is being parsed incorrectly.

Try echoing strtotime($_POST['month']) - you will probably see a result of 0.

Try using DateTime function too,

$date = new DateTime("2014 April");
echo $date->format("Y-m-d");
Ethan Webster
  • 719
  • 2
  • 6
  • 11
0

Use strptime("date","format") - you can use same format as in date()

lukasrozs
  • 123
  • 7
0

you can Use strtotime() and date()for PHP change date format:

$originalDate = "2014-04-06";
$yourDate = date("d-m-Y", strtotime($originalDate));
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38