【问题标题】:How to get timestamp parsing localized date in PHP?如何在 PHP 中获取时间戳解析本地化日期?
【发布时间】:2015-01-15 10:57:33
【问题描述】:

我需要获取 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 中的本地化日期获取时间戳有什么建议吗?

更新: 正如 cmets 中间接建议的那样,我测试了 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)

【问题讨论】:

  • 那不是strtotime吗?
  • @HaltlolXD - 不! strtotime() — 将任何 English 文本日期时间描述解析为 Unix 时间戳;问题是专门询问非英语
  • 啊抱歉;)我仍然每天都在学习!
  • stackoverflow.com/questions/6988536/… 有相同问题的人回答!
  • @HaltlolXD:谢谢,我没注意到。但是,正如 cmets 中所述,接受的答案解释了问题但没有解决它...... Hoewver,另一个答案建议strptime()php.net/manual/en/function.strptime.php)......它在 Windows 上不受支持,但我不需要它。 ..我会试试的,并会在这里发布结果...

标签: php date formatting


【解决方案1】:

最终,没有收到任何答案,我得到了这个解决方案...
它看起来有点 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");

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 2023-03-22
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多