问题
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
Why does strtotime() return false when given a unix timestamp (unless said timestamp is prefixed by @)?
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.
回答1:
The string that strtotime() receives has to be in a specific format. http://www.php.net/manual/en/datetime.formats.php
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.
For more information, here is the documentation: http://php.net/manual/en/function.strtotime.php
回答2:
Probably because strtotime converts to a UNIX timestamp, so it's not expecting one as a parameter.
Read more
回答3:
Because strtotime means string to time and your "1330725042" is not a time string but a unix timestamp wrapped in quotes.
strtotime() is meant to be used for values like "2-March-2012".
If you already have 1330725042 as a unix timestamp value, then why do you need to use strtotime()?
回答4:
strtotime() outputs timestamp, and its parameter should by text representation of date, so you are using it wrong.
回答5:
The answer is that a leading @ signifies a unix timestamp date format, whereas an integer on its own is an invalid date format. see: http://php.net/manual/en/datetime.formats.compound.php
Why strtotime('@1 a') gives a non-erroneous result of -3599 however, I have no idea. lol
回答6:
strtotime does not recognize unix timestamps but plain english dates or relative time (like "+1 day" or "1 month")
the @ before the value will surely trigger the "ignore error" mode of php within the call, (interesting side effect) thus letting the unparsable value be returned to the var dump.
回答7:
Everyone else is correct in the usage of the function and that it accepts a string and not a timestamp string. However, if you are getting results using an @ sign then likely it is an operator to signify the following text is a timestamp OR that the function is bypassing errors as per PHP's standard usage of @.
Either way, why would you want to strtotime a timestamp?
回答8:
strtotime() is meant to convert an English date format into a unix timestamp, so providing a unix timestamp instead of a date format would not be a valid use of this function. Accepted date and time formats can be seen on the php manual at http://php.net/manual/en/datetime.formats.php
来源:https://stackoverflow.com/questions/9540894/why-does-a-unix-timestamp-need-to-be-prefixed-by-for-strtotime-to-understa