3
votes

For those of who haven't take a lit class in a while, MLA is Modern Language Style and months are abbreviated like this

  • January - Jan.
  • February - Feb.
  • March - Mar.
  • April - Apr.
  • May - May
  • June - June
  • July - July
  • August - Aug.
  • September - Sept.
  • October - Oct.
  • November - Nov.
  • December - Dec.

With PHP, it's easy to get either abbreviated or not

$date = DateTime::createFromFormat('Ymd', $unformatted_date_string);
// abbreviated
echo $date->format('M d, Y');
// not abbreviated
echo $date->format('m d, Y');

But looked through http://php.net/manual/en/datetime.formats.date.php and not seeing a way to get a mix of both. Is there a better solution than string parsing?

1
You mean you're passing a sting to createFromFormat() that contains the month in both formats?Mark Baker
No, sorry that was a bit confusing. $unformatted_date_string is entered thru a datepicker, so something like 2014/11/12Shae Kuronen
What I'm currently doing is $month = $date->format('m') then running that thru a switch case and returning the month string I need. It's ok, but would be great to find a more efficient solution.Shae Kuronen
I think capital J is the one that does it, but I can't find it in the docs...airstrike

1 Answers

0
votes

You could always just format months which are greater than 4 characters in length. It seems like that's all that you really want to do.

// load array with all months for example
for ($x = 1; $x <= 12; $x++) {
    $dates[] = new DateTime('2016-' . $x . '-1');
}

// length is > 4
// if: echo abbreviated month
// else: echo unformatted month
foreach($dates as $date) {
    if (strlen($date->format('F')) > 4) {
        echo $date->format('M') . ".";
    } else {
        echo $date->format('F');
    }

    echo "\r\n";
}

Results: Jan. Feb. Mar. Apr. May June July Aug. Sep. Oct. Nov. Dec.

code on php fiddle