0

I need to enter into a SQLite table a DATETIME value. The date is originally been published with Italian month names but in different formats (see below). I need then to pre process the string before send it to the database. I thought then of using strftime that would (hopefully) manage different formats after I specify that the locale for each format is Italian. This is what I tried

<?php

error_reporting(E_ERROR | E_PARSE);

$dates = array("16:19, 23 set 2010 (CEST)", 
           "10:52, 9 dic 2006 (CEST)", 
           "19:38, Ago 16, 2005 (CEST)", 
           "12:34, Ago 8, 2005 (CEST)",
           "01:19, Apr 24, 2005 (CEST)");

print_r($dates);

foreach ($dates as $date) {
    setlocale(LC_TIME, "it_IT");
    $date = strftime($date);
    echo $date."\n";
    setlocale(LC_TIME, "en_AU");
    echo $date."\n";
}

?>

Still the dates are not converted in a time format. They are not converted at all. This is the ouput:

(
    [0] => 16:19, 23 set 2010 (CEST)
    [1] => 10:52, 9 dic 2006 (CEST)
    [2] => 19:38, Ago 16, 2005 (CEST)
    [3] => 12:34, Ago 8, 2005 (CEST)
    [4] => 01:19, Apr 24, 2005 (CEST)
)
16:19, 23 set 2010 (CEST)
16:19, 23 set 2010 (CEST)
10:52, 9 dic 2006 (CEST)
10:52, 9 dic 2006 (CEST)
19:38, Ago 16, 2005 (CEST)
19:38, Ago 16, 2005 (CEST)
12:34, Ago 8, 2005 (CEST)
12:34, Ago 8, 2005 (CEST)
01:19, Apr 24, 2005 (CEST)
01:19, Apr 24, 2005 (CEST)
CptNemo
  • 6,455
  • 16
  • 58
  • 107
  • 1
    Is there a specific reason you can't simply replace the month names? I know this is not the most elegant solution but it is one – kero Jan 05 '14 at 00:45

1 Answers1

1
$date = strftime($date);

This is not the correct usage for this function, you are passing in a date as a format which does nothing.

strftime — Format a local time/date according to locale settings

What I think you want is

strftime($date_format, strtotime($date));

However strtotime only parses dates in english (and assumes M/d/Y convention over d/M/Y)

So you will need to replace the months first

<?php

$dates = array("16:19, 23 set 2010 (CEST)",
    "10:52, 9 dic 2006 (CEST)",
    "19:38, Ago 16, 2005 (CEST)",
    "12:34, Ago 8, 2005 (CEST)",
    "01:19, Apr 24, 2005 (CEST)");


function convert_date_to_english($date_in_italian) {

    // TODO complete this table, I don't know italian sorry
    $months = array(
        'set' => 'sep',
        'dic' => 'dec',
        'apr' => 'apr',
        'ago' => 'aug'
    );

    return str_replace(array_keys($months), array_values($months), strtolower($date_in_italian));
}


foreach ($dates as $date) {
    $date = convert_date_to_english($date);
    $timestamp = strtotime($date);

    setlocale(LC_ALL, "it_IT");
    $date = strftime("%A %e %B %Y", $timestamp);
    echo $date."\n";

    setlocale(LC_ALL, 'en_AU');
    $date = strftime("%A %e %B %Y", $timestamp);
    echo $date."\n";
}
bumperbox
  • 10,166
  • 6
  • 43
  • 66