I hate PHP

in code

Gerry sent me a link to PHP: a fractal of bad design after I posted a Facebook rant. My problem is this: PHP has several functions that fetch date. Two “big” one are date() and strftime(), which have different date format syntax. WordPress uses the syntax for date(). I’m sure there are good historical reasons for this divide-somebody imported either function from an older programming language.

The Tuairisc website is presented in Irish, but administered in English. WordPress’ date_i18n() function works by the WordPress locale, not the theme or system locales. This makes sense (think about a French site on an English multisite installation), but it makes my life harder. So because of the language divide, I need a needed a way to get a date of a post based on the locale of the system. Here it is.

Why the hell does PHP have two different, incompatible date syntaxes?

 * Get Date Using System Locale Files
 * -----------------------------------------------------------------------------
 * This function mirrors get_the_date(), except it uses strftiime(), and any
 * localization supported by your system.
 * @param   string      $format      Format to use for the date.
 * @param   int         $post        ID of post whose date is needed.
 * @param   string      $locale      Locale to be used. Must be present on system!
 * @return  string                   Date in desired locale, with fallback to default locale.
 * @link https://secure.php.net/manual/en/function.strftime.php
 * @link http://www.bhalash.com/archives/13544804637

function get_the_date_strftime($format = null, $post = null, $locale = null) {
    $post = get_post($post);

    if (!$post) {
       return $false;

    if (!$locale) {
        $locale = get_option('tuairisc_fallback_locale');

    if (!$format) {
        $format = get_option('tuairisc_strftime_date_format');

    $locale = array(
        // Try to match common variants of the locale.
        $locale . '.utf8',
        $locale . '@euro',
        $locale . '@euro.utf8'

    $time = mysql2date('U', $post->post_date);

    // @link http://stackoverflow.com/a/19351555/1433400
    setlocale(LC_ALL, '');
    setlocale(LC_ALL, $locale[0], $locale[1], $locale[2], $locale[3]);

    return strftime($format, $time);

Six Months in the Mountain Kingdom

in me

Your email address will not be published. Required fields are marked *