1

如果我有一个值为: 的变量May-2012,如何将变量的格式更改为2012-05-01;

例如:

$data1 = 'May-2012';
$data2 = $data1('Y-m-01');
4

3 回答 3

4

$data2 = date( 'Y-m-d', strtotime( $data1 ) );

于 2012-05-08T13:09:21.053 回答
4

您可以使用DateTime::createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php

$data1 = DateTime::createFromFormat("F-Y", 'May-2012');
echo $data1->format("Y-m-d");
echo $data1->format("Y-m-01");

输出

2012-05-08
2012-05-01
于 2012-05-08T13:09:33.883 回答
1
$date = DateTime::createFromFormat('F-Y', 'May-2012');
$date = $date->format('Y-m-d');
于 2012-05-08T13:10:00.583 回答