I have this date (year and month) 201511
and I need to show in this format: 112015
(month and year).
I've tried this: date('m/Y', strtotime($date))
, but does not work.
I have this date (year and month) 201511
and I need to show in this format: 112015
(month and year).
I've tried this: date('m/Y', strtotime($date))
, but does not work.
You have to read in the format you have and do that properly. date('m/Y')
is not the format you feed into that function.
Read http://php.net/manual/datetime.createfromformat.php
or http://php.net/manual/function.date.php if you really have to use date()
.
m and n | Numeric representation of a month, with or without leading zeros | 01 through 12 or 1 through 12
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003
$dateStringOldFormat = '201511';
$dateStringNewFormat = DateTime::createFromFormat('Ym', $dateStringOldFormat)->format('mY');
If this were me, I would just use some string tools to get the values.
Demo: https://3v4l.org/1LMVj
$d = 201511;
$year = substr($d,0,4);
$month = substr($d,-2);
$reverse = $month.$year;
echo $reverse;
// 112015
If you have Full Date. Conversion is simple. otherwise in this Format you can use this method.
echo dateFormat('201511'); // Your Format
function dateFormat($input){
$date=substr($input, 0,4).'/'.substr($input, 4,5).'/13'; // add extra date grater than 12
return date('mY',strtotime($date));// convert and return
}
Thank you.