-5

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.

user115812
  • 9
  • 2
  • 7
  • 7
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php). This question has been answered with very good and definitive answers at least 50 times by now. With all due respect, but do some research before asking questions like these. – Loek Jun 29 '18 at 12:12
  • Possible duplicate of [PHP reformat date before POST](https://stackoverflow.com/questions/6811323/php-reformat-date-before-post) – michail_w Jun 29 '18 at 12:12
  • 1
    https://stackoverflow.com/a/35473547/2943403 – mickmackusa Jun 29 '18 at 12:14
  • 2
    @michail_w that is a poor/unsuitable dupe link. Please be more careful. – mickmackusa Jun 29 '18 at 12:15
  • Please accept my appologies, but I tried using these solutions and my date does not have day and these solutions does not working for me – user115812 Jun 29 '18 at 12:16
  • @user115812: Don't be. You have given links. Go through that links and make some efforts. You will get an answer. :) – Virb Jun 29 '18 at 12:20
  • Ok, I will delete this post and once again accept my appologies – user115812 Jun 29 '18 at 12:22
  • Is there a good reason to not use a simple string manipulation? (Non-date function) – mickmackusa Jun 29 '18 at 12:22
  • Your solution doesn't work because `201511` is not a format `strtotime` knows. – user3783243 Jun 29 '18 at 12:28
  • what exactly do you want the answer to be ? –  Jun 29 '18 at 12:36
  • @dean the question states that month and date must be in opposite positions. Ignore the slash in the coding attempt. – mickmackusa Jun 29 '18 at 12:44
  • @mickmackusa i gave that answer and i was flagged, so i was puzzled. –  Jun 29 '18 at 12:52
  • @dean your deleted answer was useless. It was processing today's date. That is not the issue. – mickmackusa Jun 29 '18 at 12:54

3 Answers3

5

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');
Community
  • 1
  • 1
steros
  • 1,794
  • 2
  • 26
  • 60
  • Please make sure you are linking to the English php.net, simply as this is English StackOverflow and there are other languages available for those who do not seak English :) – Can O' Spam Jun 29 '18 at 12:25
  • 1
    ah sorry it always redirects to the german page as it is my locale...I'll correct it – steros Jun 29 '18 at 12:25
  • you can also use a link without the language part http://php.net/manual/datetime.createfromformat.php and it will redirect to the locale of the user – ᴄʀᴏᴢᴇᴛ Jun 29 '18 at 12:36
0

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
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Richard Housham
  • 1,525
  • 2
  • 15
  • 31
-1

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.