Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to month name & year

Tags:

php

mysql

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
like image 460
Nitish Kumar Diwakar Avatar asked Jul 17 '17 07:07

Nitish Kumar Diwakar


2 Answers

No Need of DATE_FORMAT() function.

Example-1: If 00 used in day. Then, output will be June, 2017

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

Example-2: If 01 or valid day used in day. Then, output will be July, 2017

<?php
$date = '2017-07-01';
echo date('F, Y', strtotime($date)); //July, 2017
?>
like image 69
Nana Partykar Avatar answered Oct 06 '22 05:10

Nana Partykar


You are not using correct parameters, use F for moth and Y for year

Full code:

$date = '2017-07-00';
$date = date('F, Y ', strtotime($date));
echo $date;
like image 20
Suneel Kumar Avatar answered Oct 06 '22 04:10

Suneel Kumar