为什么 Unix 时间戳需要在 strtotime() 前面加上“@"前缀才能理解? [英] Why does a unix timestamp need to be prefixed by '@' for strtotime() to understand it?

查看:54
本文介绍了为什么 Unix 时间戳需要在 strtotime() 前面加上“@"前缀才能理解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 strtotime() 以一种奇怪的方式处理 unix 时间戳,我很好奇是否有人知道这是为什么:

I noticed that strtotime() handles unix timestamps in an odd way, and I was curious if anyone knows why that is:

var_export(strtotime('1330725042'));  // false
var_export(strtotime('@1330725042')); // 1330725042

为什么在给定 unix 时间戳时 strtotime() 返回 false(除非所述时间戳以 @ 为前缀)?

Why does strtotime() return false when given a unix timestamp (unless said timestamp is prefixed by @)?

这是我构建的库方法的内部结构,旨在将未知格式的变量解析"为时间戳.在这种情况下,使用裸 strtotime() 没有帮助,因为当传入的值实际上是时间戳时,它会返回错误的结果.

This is from the internals of a library method that I built that is intended to "resolve" an unknown-format variable into a timestamp. Using a bare strtotime() isn't helpful in this case because it returns the wrong result when the incoming value actually is a timestamp.

我已经重新设计了库方法来对类似时间戳的值进行显式检查并将其返回未经修改,所以......我们可以说,这个问题不再有实际应用;我只是好奇.

I've reworked the library method to do an explicit check for a timestamp-like value and return it unmodified, so there's... shall we say, no practical application for this question anymore; I'm just curious.

推荐答案

strtotime() 接收的字符串必须采用特定格式.http://www.php.net/manual/en/datetime.formats.php

The string that strtotime() receives has to be in a specific format. http://www.php.net/manual/en/datetime.formats.php

如果strtotime() 无法识别输入格式,则返回FALSE.由于 unix 时间只是一个数字序列,除非您明确指定所使用的格式,否则它无法正确解析它们.将 @ 放在开头给函数指示此序列为 unix 时间格式.

If strtotime() does not recognize the input format, it returns FALSE. As unix time is just a sequence of numbers, it cannot properly parse them unless you explicitly specify what format you are using. Putting an @ at the start gives the function the instruction that this sequence is in unix time format.

有关更多信息,请参阅文档:http://php.net/manual/en/function.strtotime.php

For more information, here is the documentation: http://php.net/manual/en/function.strtotime.php

这篇关于为什么 Unix 时间戳需要在 strtotime() 前面加上“@"前缀才能理解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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