将ISO日期转换为Unix时间戳的最有效的方法?

将ISO日期转换为Unix时间戳的最有效的方法?

问题描述:

在我的应用程序中,我有一些记录需要根据一些时间参数进行过滤。要进行日期比较,我需要将我的记录中的日期(格式为YYYY-MM-DD的字符串)转换为unix时间戳(自1970年以来的几秒)。因为我有成千上万的记录,我真的想找到最有效的方法。

In my application, I have some records which I need to filter based on some time parameters. To do the date comparisons, I need to convert the dates in my records (a string in the format YYYY-MM-DD), to a unix timestamp (seconds since 1970). Since I have many thousands of records, I really want to find the most efficient way to do it.

另一个问题建议在数据库中进行操作(不是这里的一个选项,可悲的是),使用 strtotime() strptime(),但是这些不要像你那样记忆力和时间效率最高的方法感觉?

Answers to another question suggested doing it in the database (not an option here, sadly), using strtotime() or strptime(), but these don't feel like the most memory- and time-efficient methods, you know?

鉴于我知道输入的确切格式,可能会使用某种形式的字符串操作( substr explode )结合 mktime 更好?

Given that I know the exact format of my input, would perhaps using some form of string manipulation (substr, or explode) combined with mktime be better?

如果你认为我的优化过早,那么只是幽默我好吗?

If you believe I'm being premature in my optimisation, then just humour me ok?

认为strtotime()不是一个有效的函数?你做过任何基准请记住,strtotime()是一个内置库函数,它将直接写入C中,而任何您将通过php解释器运行。

Why do you think strtotime() is not an efficient function? Have you done any benchmarks. Keep in mind that strtotime() is a builtin library function which will is written directly in C, whereas anything you come up will run through the php interpreter.

还要记住,日期计算本来就是棘手的,对于像闰年和时区这样的问题。

Also keep in mind that date calculations are inherently tricky, do to issues like leap years and timezones.

对于另一种更快的方法,我认为使用substr()进行解析将比正则表达式更快。将结果输入到mktime()中,然后设置。

As for an alternate faster method, I think using substr() to parse will be faster than a regex. Feed the results in to mktime() and you're set.