strtotime()认为有害吗? [英] strtotime() considered harmful?

查看:127
本文介绍了strtotime()认为有害吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎很多人在PHP中遇到日期/时间问题,并且不可避免地,许多被接受的答案往往是以这种方式使用 strtotime

It seems like a lot of people struggle with date/time issues in PHP, and inevitably, many of the accepted answers tend to be "Use strtotime in this way."

这是真的最好的办法直接处理日期问题的人?我开始感觉像 strtotime 是一种非常有趣的技巧,不一定要依赖于重要的日期/时间计算,而且它的性质是任意的字符串,它似乎是一个潜在的bug,很难预测的行为。不能区分MM / DD / YYYY和DD / MM / YYYY是一件很大的事情,不是吗?

Is this really the best way to direct people dealing with date problems? I'm beginning to feel like strtotime is sort of a nifty trick that shouldn't necessarily be relied on for important date/time calculations, and by the nature of it taking arbitrary strings, it seems like a potential source of buggy, hard-to-predict behavior. Its inability to differentiate between MM/DD/YYYY and DD/MM/YYYY is sort of a big deal, no?

StackOverflow通常非常有利于推广良好做法我很少看到没有人说改用PDO的 mysql_real_escape_string 对话。)

StackOverflow is usually very good at promoting good practices (I rarely see a mysql_real_escape_string conversation that doesn't have someone say "Use PDO instead.")

但是在PHP中,日期问题似乎并不是一个被接受的规范,很多人掉落在拐角处,就是 strtotime

But there doesn't seem to be an accepted norm around date issues in PHP, with a lot of people falling back on the crutch that is strtotime.

那么,我们该怎么做,如果有的话呢?有没有更好的规范,我们应该为人们提出问题,例如如何添加1周X,或如何将此日期格式转换为其他日期格式?

So, what should we be doing about this, if anything at all? Is there a better norm we should be enforcing for people asking questions like "How do I add 1 week to X", or "How do I convert this date format to this other date format?"

如何处理日期/时间问题(如 strtotime )的最佳,最可靠的方法是尝试,但往往无法执行?

What is the best, most reliable way to deal with Date/Time issues like strtotime tries to, but too often fails to?

推荐答案

我将开始说我是使用DateTime对象的大提倡者,它允许您使用 DateTime :: createFromFormat() 功能。 DateTime对象使代码更加易读,避免了使用60 * 60 * 24进行整个Unix时间戳修改来提前日期。

I'll start off by saying that I am a big advocate for using the DateTime object, which allows you to use the DateTime::createFromFormat() function. The DateTime object makes the code much more readable and avoids the need to do the whole Unix timestamp modification using 60 * 60 * 24 to advance the date days.

这就是说,strtotime()采用的任意字符串不是很难预测。 支持的日期和时间格式列出了支持的格式。

That being said, the arbitrary string that strtotime() takes is not very hard to predict. Supported Date and Time Formats lists the formats that are supported.

根据您无法区分MM / DD / YYYY和DD / MM / YYYY的示例,它根据日期格式。使用斜杠的日期总是以美式格式读取。所以00/00/0000格式的日期将永远读为MM / DD / YYYY。或者使用破折号或周期将是DMY。例如00-00-0000将永远读为DD-MM-YYYY。

As per your example of not being able to differentiate between MM/DD/YYYY and DD/MM/YYYY it does differentiate based on the Date Formats. Dates that use slashes are always read as American format. So a date in the format 00/00/0000 will always be read as MM/DD/YYYY. Alternatively using dashes or periods will be DMY. e.g. 00-00-0000 will always be read as DD-MM-YYYY.

以下是一些示例:

<?php
$dates = array(
    // MM DD YYYY
    '11/12/2013' => strtotime('2013-11-12'),
    // Using 0 goes to the previous month
    '0/12/2013' => strtotime('2012-12-12'), 
    // 31st of November (30 days) goes to 1st December
    '11/31/2013' => strtotime('2013-12-01'), 
    // There isn't a 25th month... expect false
    '25/12/2013' => false,

    // DD MM YYYY
    '11-12-2013' => strtotime('2013-12-11'),
    '11.12.2013' => strtotime('2013-12-11'),
    '31.12.2013' => strtotime('2013-12-31'),
    // There isn't a 25th month expect false
    '12.25.2013' => false,
);

foreach($dates as $date => $expected) {
    assert(strtotime($date) == $expected);
}

正如你可以看到几个关键的例子是 25/12/2013 12.25.2013 如果读取相反的格式,则它们都有效,但是返回 false ,因为它们根据排除的日期和时间格式无效...

As you can see a couple of key examples are 25/12/2013 and 12.25.2013 which would both be valid if read the with the opposite format, however they return false as they are invalid according the the Suported Date and Time Formats...

所以你可以看到行为是相当可预测的。一如以往,如果您从使用输入收到日期,您应该首先验证该输入。如果您没有首先验证输入,则方法不起作用。

So as you can see the behaviour is quite predictable. As always if you are receiving a date from use input you should be validating that input first. No method will work if you are not validating the input first.

如果您想要非常具体地了解正在阅读的日期,或者您所提供的格式, t支持的格式,那么我建议使用 DateTime :: createFromFormat()

If you want to be very specific about the date being read, or the format you are being given isn't a supported format, then I recommend using DateTime::createFromFormat().

这篇关于strtotime()认为有害吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆