oneseven9955/dateparser

DateParser 是一个可以解析多种格式的日期字符串的解析器。

0.1.0 2024-07-30 16:44 UTC

This package is auto-updated.

Last update: 2024-09-30 17:26:01 UTC


README

此 PHP 包是一个日期字符串解析器,可以解析多种格式的日期字符串。它使用有限状态机实现。使用此包,开发者可以轻松解析日期字符串,无需依赖外部库或工具。

入门指南

use OneSeven9955\DateParser\DateParser;
use OneSeven9955\DateParser\ParseException;

// Strict parse: an invalid date string will cause ParseException
try {
    $datetime = DateParser::from('30/04/2025')->parse(); // DateTime { date: 2015-04-30 00:00:00.0 UTC (+00:00) }
} catch (ParseException $ex) {
    printf("Unable to parse the date: %s\n", $ex->getMessage());
}

// Silent parse: an invalid date string will cause null as return value
$datetimeOrNull = DateParser::from('30#04#2025')->parseSilent(); // null

支持的日期格式

时间部分将被省略。

YYYY-MM-DD

  • 2014-04-26

MM/DD/YY

  • 3/31/2014
  • 03/31/2014
  • 08/21/71
  • 8/1/71

YYYY/MM/DD

  • 2014/3/31
  • 2014/03/31

MM.DD.YY

  • 3.31.2014
  • 03.31.2014
  • 08.21.71
  • 2014.03
  • 2014.03.30

文本月份

  • oct 7, 1970
  • oct 7, '70
  • oct. 7, 1970
  • oct. 7, 70
  • October 7, 1970
  • October 7th, 1970
  • 7 oct 70
  • 7 oct 1970
  • 03 February 2013
  • 1 July 2013
  • 2013-Feb-03