简体   繁体   English

将日期转换为月份名称和年份

[英]Convert date to month name & year

I am trying to convert a date to month name and year. 我正在尝试将日期转换为月份名称和年份。

$date = '2017-07-00';
$date = date('m/y', strtotime($date));
echo DATE_FORMAT($date, '%M %Y');

I am expecting output like 我期待输出像

July, 2017

Here is error i am getting 这是我得到的错误

Warning: date_format() expects parameter 1 to be DateTimeInterface, string given

No Need of DATE_FORMAT() function. 不需要DATE_FORMAT()函数。

Example-1 : If 00 used in day. 示例1 :如果在白天使用00。 Then, output will be June, 2017 然后,输出将是2017年6月

<?php
$date = '2017-07-00';
echo date('F, Y', strtotime($date)); //June, 2017
?>

Example-2 : If 01 or valid day used in day. 示例2 :如果在白天使用01或有效日期。 Then, output will be July, 2017 然后,输出将是2017年7月

<?php
$date = '2017-07-01';
echo date('F, Y', strtotime($date)); //July, 2017
?>

You are not using correct parameters, use F for moth and Y for year 您没有使用正确的参数,使用F表示飞蛾, Y表示年份

Full code: 完整代码:

$date = '2017-07-00';
$date = date('F, Y ', strtotime($date));
echo $date;

Try This. 尝试这个。

$date = '2017-07-01';
$date = date('F, Y', strtotime($date));

echo $date;

你可以用它

echo date('F,Y',strtotime($date));

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

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