简体   繁体   中英

PHP change date format

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.

Use DateTime

$date = new DateTime("2014 April");
echo $date->format("Y-m-d");
$date = DateTime::createFromFormat('Y F', '2014 April');
echo $date->format('Y-m-01');

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");

使用strptime(“日期”,“格式”) - 您可以使用与date()相同的格式

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

$originalDate = "2014-04-06";
$yourDate = date("d-m-Y", strtotime($originalDate));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM