簡體   English   中英

將荷蘭時間字符串轉換為時間戳php

[英]Convert dutch time string to timestamp php

我有一個函數,可以在第一行的文件中讀出日期。 這個日期格式化為荷蘭像這樣的2 mei 201328 jun. 2013 28 jun. 2013

它需要將日期字符串轉換為時間戳,但無論我嘗試它,都不適用於mei飛蛾或任何其他荷蘭月份。 這是我目前的代碼(原始函數是更多的代碼,但這是它出錯的地方)

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

現在,使用此函數時會出現一些結果:

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

但是,當我把這個代碼放在函數中時

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

它打印'2 mei 2013'

我怎么能告訴php不僅用荷蘭語格式化日期時間字符串,還用荷蘭語讀取它?

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

感謝下面的一些解釋,我現在有代碼工作(這是完整的功能)

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;
}

在不創建自己的日期解析器的情況下,本機PHP函數僅使用英語日期。

但是,有一個適用於PHP的國際dateformatter擴展。 您可以安裝此插件,然后就可以解析非英語日期。

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

正如 其他人 發現的那樣strtotime不尊重設置的語言環境。 實際上,它在手冊中的描述是:“將任何英文文本日期時間描述解析為Unix時間戳”

解決方案

  • 您可以使用strptime()因為PHP5 確實尊重語言環境(如strftime ),但有一些關於在php網站上使用它的警告。

  • 您可以編寫一個函數,將荷蘭月份名稱替換為英文月份名稱,然后調用strtotime

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM