Formatting Months With AP Stlye

logo_AP_newThe PHP date() class supports a mess-load of different date masks, but developers working with content creators and editors from a journalism background may find the date() class wanting, particularly when it comes to supporting style and usage guides like the AP Stylebook. In particular, AP Style calls for longer month names (Jan., Feb., Aug. Sept. Oct., Nov., Dec.) to be abbreviated, while shorter month names (March, April, May, June, July) remain intact.

I tackled similar AP Style issue realated to how str

So, here’s a quick and dirty function to convert “whole” months to AP Style months:

<?php
//Function to reformat dates (months) to comply with AP Style.
function APdate($datestring){
$APstyleDate = (str_replace(array(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
"Mar.",
"Apr.",
"Jun.",
"Jul.",
"Sep.",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
),
array(
"Jan.",
"Feb.",
"March",
"April",
"May",
"June",
"July",
"Aug.",
"Sept.",
"Oct.",
"Nov.",
"Dec.",
"March",
"April",
"June",
"July",
"Sept.",
"Jan.",
"Feb.",
"March",
"April",
"May",
"June",
"July",
"Aug.",
"Sept.",
"Oct.",
"Nov.",
"Dec."
), $datestring));
return $APstyleDate;
}
?>

Preserve Your Data’s International Flavour

funky

When working with MySQL data sources that contains international characters (i.e., accents, Asian characters), it’s important that form data is entered using UTF-8 encoding and stored in a UTF-8 compliant table. It’s easy to overlook that the data also needs to be pulled out of MySQL using UTF-8, since the default database engine encoding may be set to a local standard like ASCII, even if a particular database or table is set to UTF-8 (or other flavor of Unicode).

Preventing international characters stored in MySQL from going all funky on output is actually pretty simple:

In PHP, we would need to initiation the connection to MySQL in the usual way:

$myQuery=mysql_connect ("mysql.exampledomain.com:3306", "username", "password");

But then add the following line:

mysql_set_charset("UTF8", $myQuery);

Now, all that lovely data should return with all its international flavour intact.

There’s more information about this technique over at Stack Overflow.