PHP date_format(): 如何从字符串值格式化日期
PHP date_format(): How to format date from string value
我有一点PHP代码:
$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
用于格式化日期。预期输出将是 2015-12-01
但它是 returns 2016-12-01
。我错过了什么?
先使用createFromFormat
方法,提供输入格式:
$exd = DateTime::createFromFormat('d M, Y', '01 Dec, 2015');
// arguments (<format of the input>, <the input itself>)
$exd = date_format($exd, 'Y-m-d'); // then choose whatever format you like
echo $exd;
date_create()
函数只接受参数link,这个函数也是DateTime::__construct()
的别名函数
检查函数 date_create_from_format()
它也是 DateTime::createFromFormat() 的别名函数。参考 link
$exd = date_create_from_format('j M, Y', '01 Dec, 2015');
//$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
我得到了你的错误的解决方案
即 date_format(datae_variable,date_format);
<?php
$exd = date_create('01 Dec, 2015');
$exd1 = date_format($exd,"Y-m-d");//here you make mistake
echo $exd1;
?>
可以简单的调用一个日期函数。将 stringtotime 用于 exact/precise date/time 值
date("Y-m-d",strtotime("01 Dec 2015"))
当您 运行 此代码时,输出将显示
2015-12-01
这是因为字符串中的逗号在编译器中终止了日期字符串。如果您准确指定时区(如
$timezone = 'America/New_York
)。参数你也可以显示精确的时间。
我有一点PHP代码:
$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
用于格式化日期。预期输出将是 2015-12-01
但它是 returns 2016-12-01
。我错过了什么?
先使用createFromFormat
方法,提供输入格式:
$exd = DateTime::createFromFormat('d M, Y', '01 Dec, 2015');
// arguments (<format of the input>, <the input itself>)
$exd = date_format($exd, 'Y-m-d'); // then choose whatever format you like
echo $exd;
date_create()
函数只接受参数link,这个函数也是DateTime::__construct()
检查函数 date_create_from_format()
它也是 DateTime::createFromFormat() 的别名函数。参考 link
$exd = date_create_from_format('j M, Y', '01 Dec, 2015');
//$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
我得到了你的错误的解决方案 即 date_format(datae_variable,date_format);
<?php
$exd = date_create('01 Dec, 2015');
$exd1 = date_format($exd,"Y-m-d");//here you make mistake
echo $exd1;
?>
可以简单的调用一个日期函数。将 stringtotime 用于 exact/precise date/time 值
date("Y-m-d",strtotime("01 Dec 2015"))
当您 运行 此代码时,输出将显示
2015-12-01
这是因为字符串中的逗号在编译器中终止了日期字符串。如果您准确指定时区(如
$timezone = 'America/New_York
)。参数你也可以显示精确的时间。