35

I have this date data :

20130814

How to get string output like this : (in PHP)

"August 2013"

user2750491
  • 395
  • 1
  • 3
  • 3
  • 1
    Have you even tried to read about [`date`](http://php.net/manual/es/function.date.php) function? – Alvaro Sep 05 '13 at 11:25
  • Use [`substr`](http://www.php.net/substr), [`mktime`](http://www.php.net/mktime) and [`date`](http://www.php.net/date). I'm sure you can figure the rest on your own. – h2ooooooo Sep 05 '13 at 11:26
  • 1
    do some research first, then if it doesn't work, ask questions – slash197 Sep 05 '13 at 11:26

3 Answers3

87

You could use:

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

which should do the trick.

Edit: You have a date which is in a string format. To be able to format it nicelt, you first need to change it into a date itself - which is where strtotime comes in. It is a fantastic feature that converts almost any plausible expression of a date into a date itself. Then we can actually use the date() function to format the output into what you want.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • 1
    +1 for correct answer, and the fact that you were downvoted for some odd reason? – h2ooooooo Sep 05 '13 at 11:37
  • Hmmm, yeah, thanks. guess I was downvoted for not providing a full explanation. Added an edit explaining what I did. – Fluffeh Sep 05 '13 at 11:39
  • What happened to having a quality answer for a quality question, but a quick answer for an obvious non-researched question? – h2ooooooo Sep 05 '13 at 12:02
  • @h2ooooooo Well, yes and no. I wrote a huge [Q&A](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) just to be able to link to something in more detail when posting about SQL queries, but it was a fair downvote. I should have explained it - the next person who comes across this question should get an answer that stands on its own :) – Fluffeh Sep 05 '13 at 12:05
20

I think your date data should look like 2013-08-14.

<?php
 $yrdata= strtotime('2013-08-14');
    echo date('M-Y', $yrdata);
 ?>
// Output is Aug-2013
Mawia HL
  • 3,605
  • 1
  • 25
  • 46
20

if you want same string output then try below else use without double quotes for proper output

$str = '20130814';
  echo date('"F Y"', strtotime($str));

//output  : "August 2013" 
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44