简体   繁体   English

在PHP中更改变量的日期格式

[英]Changing the date format of a variable in php

If I've got a variable with the value: May-2012 , how do I change the format of the variable to 2012-05-01 ; 如果我有一个变量值: May-2012 ,如何将变量的格式更改为2012-05-01

eg: 例如:

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

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

You can use DateTime::createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php 您可以使用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");

Output 输出量

2012-05-08
2012-05-01
$date = DateTime::createFromFormat('F-Y', 'May-2012');
$date = $date->format('Y-m-d');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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