DateTime::createFromFormat 在 PHP <5.3.0

2024-05-10php开发问题
7

本文介绍了DateTime::createFromFormat 在 PHP <5.3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一个与 DateTime::createFromFormat 但我需要它在运行早于 v5.3 的 PHP 版本的环境中工作.基本上我需要提供一种格式,就像你使用 Date() 函数,然后我需要针对该格式解析/验证字符串,如果字符串格式正确且日期有效,则返回时间戳.

I'm looking for a function identical to DateTime::createFromFormat but I need it to work in an environment running a version of PHP which is older than v5.3. Basically I need to provide a format, like you'd use with the Date() function, and I then need to parse/validate a string against that format, and return a timestamp if the string is formatted correctly and a valid date.

谁知道我在哪里可以找到类似的东西,还是我必须自己写?

Anyone know where I can find something like that, or do I have to write it myself?

同样,这必须使用作为参数提供的特定格式.格式可以是任何东西,所以不能保证我可以只使用 strtotime().

Again, this has to work with a specific format, provided as an argument. The format could be anything, so there's no guarantee I can just use strtotime().

推荐答案

DateTime::createFromFormatdate_parse_from_format 已添加到 PHP 5.3 中,因为对该功能的需求很高,尤其是为不使用美国日期/时间格式的用户编写代码的开发人员.

DateTime::createFromFormat and date_parse_from_format have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.


在此之前,您必须开发一个特定的函数来解析您使用的格式;与 PHP <5.3、一般做的是:


Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :

  • 决定应用程序将接受哪种格式
  • 显示一些消息,例如 您的输入应该是 JJ/MM/AAAA" (DD/MM/YYYY 的法语)
  • 检查输入是否正确,关于该格式
  • 并解析它以将其转换为 PHP 可以理解的日期/时间.

这意味着应用程序和开发人员通常不允许使用那么多格式,因为每种格式都意味着一个不同的附加验证+解析功能.

Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.


如果你真的需要那种允许任何可能格式的函数,恐怕你必须自己写:-(


If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(

如果您了解 C 代码,也许看看 date_parse_from_format 的来源会有所帮助?它应该在 ext/date/php_date.c 之类的东西中——但似乎没有那么简单:它正在调用 timelib_parse_from_format 函数,该函数在 ext/data/lib/parse_date.c,看起来不太友好^^

Maybe taking a look at the sources of date_parse_from_format could help, if you understand C code ? It should be in something like ext/date/php_date.c -- but doesn't seem to be that simple : it's calling the timelib_parse_from_format function, which is defined in ext/data/lib/parse_date.c, and doesn't look that friendly ^^

这篇关于DateTime::createFromFormat 在 PHP &lt;5.3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17