Skip to content
Advertisement

Is it possible to convert a timestamp to human readable date (in any language) with CodeIgniter 3.1.9

Is it possible to convert a timestamp to human readable date (in any language) with CodeIgniter 3.1.9.

As much as possibe, I would like to use helper (date helper).

  • Input : 2018-11-01 17:12:26.
  • Output : le 1er novembre 2018

Advertisement

Answer

Date helper will not help you. Use the following function (also include conversion to local time):

function dateTimeToFrench($datetime, $format="l j F Y à H:i:s", $convertToLocalTime = false)
{
    $english_days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
    $french_days = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
    $english_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    $french_months = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');

    $datetime = new DateTime($datetime, new DateTimeZone('UTC'));
    if ($convertToLocalTime) $datetime->setTimezone(new DateTimeZone('Europe/Paris'));
    return str_replace($english_months, $french_months, str_replace($english_days, $french_days, $datetime->format($format) ) );
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement