4

I have a function that reads out the date in a file on the first line. This date is formatted in dutch like this 2 mei 2013 or 28 jun. 2013

It needs to convert the date string into a timestamp, but whatever i try it won't work for the mei moths or any other dutch named month. Here is the code I currently have (the original function is a bit more code, but this is where it goes wrong)

function getTimestamp($date){
    date_default_timezone_set('Europe/Amsterdam');
    setlocale(LC_ALL, 'nl_NL');
    $timestamp = strtotime($date);
    return $timestamp;
}

Now, here are some results when using this function:

$timestamp = getTimestamp('28 jun. 2013') //1372370400
$timestamp2 = getTimestamp('2 mei 2013') // false

but, when i put this code in the function

echo strftime('%e %b %Y', 1367445600)."\n";

it prints '2 mei 2013'

How can I tell php not only format the date-time string in Dutch, but also read it in Dutch?

=======================

Thanks to some explanation below I now have the code working (this is the full function)

public function getReportDate(){
    $mothsTranslated = array('mrt'=> 'mar','mei'=>'may', 'okt'=>'oct');
    $content = file($this->file);
    $line = $content[0];
    $header = str_getcsv($line, $this->delimiter);
    $date = str_replace('.', '', $header[1]);
    foreach ($mothsTranslated as $dutch => $eng) {
        if(strpos($date, $dutch) !== false){
            $date = str_replace($dutch, $eng, $date);
        }
    }
    $timestamp = strtotime($date);

    return $timestamp;
}
MegaWubs
  • 624
  • 1
  • 8
  • 17

2 Answers2

2

Without creating your own date parser, the native PHP functions only use English dates.

However, there is an international dateformatter extension available for PHP. You can install this plugin and then would be able to parse non-english dates.

http://www.php.net/manual/en/intldateformatter.parse.php

Schleis
  • 41,516
  • 7
  • 68
  • 87
1

As others found out, strtotime does not respect the set locale. Indeed, it's description in the manual states: "Parse about any English textual datetime description into a Unix timestamp"

Solutions

  • You can use strptime() since PHP5 that does respect the locale (like strftime), but there are some warnings about using it on the php website.

  • You could write a function that replaces the Dutch month names to English month names and then calls strtotime.

Community
  • 1
  • 1
catchmeifyoutry
  • 7,179
  • 1
  • 29
  • 26
  • Thanks for the explanation. I've added the translate functionality to the function and will update my question with the working code. – MegaWubs Aug 09 '13 at 07:29