如何在PHP中获取时间戳解析本地化日期?

时间:2015-01-15 10:57:33

标签: php date formatting

我需要让UNIX时间戳解析用特定语言环境编写的非数字日期 示例日期(意大利语语言环境)是:

2014 Settembre 25, 19:12:55

这是我目前编写的代码,但它仅适用于英语语言环境...:

<?php

  $format = "Y M d, H:i:s";
  $timezone = "Europe/Rome"; # (timezone is not meaningful for this example...)

  $date = "2014 September 25, 19:12:55";
  $locale = "en";
  print date2Timestamp($format, $locale, $timezone, $date);
  # outputs "1411621975", o.k.

  $date = "2014 Settembre 25, 19:12:55";
  $locale = "it";
  print date2Timestamp($format, $locale, $timezone, $date);
  # outputs "PHP Fatal error:  Call to a member function getTimestamp() on a non-object", error!

  /**
    * Converts a date from a custom format to UNIX timestamp.
    *
    * @param string $format   source date format
    * @param string $locale   source locale
    * @param string $timezone source timezone
    * @param string $date     source date to be converted
    * @return string          UNIX timestamp conevrsion of the given date
    */
  function date2Timestamp($format, $locale, $timezone, $date) {
    $oldLocale = setlocale(LC_TIME, 0);
    setlocale(LC_TIME, $locale);
    $dt = DateTime::createFromFormat($format, $date, new DateTimeZone($timezone));
    setlocale(LC_TIME, $oldLocale);
    return $dt->getTimestamp();
  }

?>

因此,有效地,如文档所述,PHP DateTime没有考虑 locales ... :-(

如何在PHP中从本地化日期获取时间戳?

更新: 正如评论中间接建议的那样,我测试了strptime() 它似乎没有按预期工作,甚至英语格式的日期......: - (

$ php --version
PHP 5.3.3 (cli) (built: Oct 30 2014 20:12:53)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

<?php
  $locale = "en";
  setlocale(LC_TIME, $locale);

  $format = "%Y %m %d, %H:%M:%S";
  $date = "2014 9 25, 19:12:55";
  print "strptime with decimal month:"; var_dump(strptime($date, $format));

  $format = "%Y %F %d, %H:%M:%S";
  $date = "2014 September 25, 19:12:55";
  print "strptime with textual month:": var_dump(strptime($date, $format));
?>

输出:

strptime with decimal month: array(9) {
  ["tm_sec"]=>
  int(55)
  ["tm_min"]=>
  int(12)
  ["tm_hour"]=>
  int(19)
  ["tm_mday"]=>
  int(25)
  ["tm_mon"]=>
  int(8)
  ["tm_year"]=>
  int(114)
  ["tm_wday"]=>
  int(4)
  ["tm_yday"]=>
  int(267)
  ["unparsed"]=>
  string(0) ""
}
strptime with textual month: bool(false)

1 个答案:

答案 0 :(得分:0)

最终,没有得到任何答案,我得到了这个解决方案...... 它看起来有点hacky,但它确实有效:

  /**
    * Converts a *localized* date from format
    * "Year MonthName day, hour:minute:second" to UNIX timestamp
    *
    * @param string $date     source date to be converted
    * @return string          UNIX timestamp conevrsion of the given date
    */
  function date2Timestamp($date) {
    $timestamp = "0";
    for ($m = 1; $m <= 12; $m++) {
      $month_name = ucfirst(strftime("%B", mktime(0, 0, 0, $m))); # get month name for current locale
      if (strstr($date, $month_name)) {
        $date = str_replace($month_name, $m, $date); # replace month name with month number
        $date = preg_replace("/^(\d{4})\s+(\d{1,2})\s+(\d{1,2})/", "$1-$2-$3", $date); # puts dashes to separate Y M D, so strtotime is happy...
        $timestamp = strtotime($date);
        break;
      }
    }
    return $timestamp;
  }

显然假定语言环境和时区已经正确初始化...例如:

setlocale(LC_TIME, "it_IT");
date_default_timezone_set("Europe/Rome");