MLA style month abbreviation with PHP date format?

For those who didn't take a lighted class, MLA is modern language style and months are shortened like

  • January - January.
  • February - Feb
  • March - March.
  • April - April.
  • May - May
  • June - June
  • July - July
  • August - August.
  • September - September.
  • October - October.
  • November - November.
  • December - December.

With PHP, it's easy to get either shorthand 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 at http://php.net/manual/en/datetime.formats.date.php and didn't see a way to get a mixture of both. Is there a better solution than parsing strings?

+3


source to share


1 answer


You can always just format months longer than 4 characters. This seems to be all 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.

php script code

+1


source







All Articles