6

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.

6
  • 5
    Because the documentation says so :) php.net/manual/en/datetime.formats.compound.php I don't think a integer is a valid Date/Time format
    – Tchoupi
    Commented Mar 2, 2012 at 21:58
  • 1
    +1 for that link Mathieu. I figured there were docs on it somewhere.
    – Josh
    Commented Mar 2, 2012 at 22:02
  • Thanks. I actually had no idea that a unix timestamp could be a valid PHP DateTime format.
    – Tchoupi
    Commented Mar 2, 2012 at 22:04
  • 2
    @Mathieu - post that as an answer because it actually answers the second part of the question
    – Mark Baker
    Commented Mar 2, 2012 at 22:04
  • 1
    @dqhendricks I was wondering when someone would ask that (: I updated my question with some more info.
    – user212218
    Commented Mar 2, 2012 at 22:16

8 Answers 8

6

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

1
  • 2
    Thanks for the links. I found this: 'Unix Timestamp "@" "-"? [0-9]+ "@1215282385"' at php.net/datetime.formats.compound
    – user212218
    Commented Mar 2, 2012 at 22:18
2

Probably because strtotime converts to a UNIX timestamp, so it's not expecting one as a parameter.

Read more

1

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()?

2
  • 1
    WRT why call strtotime() on a timestamp, the motivation behind this question is situations where the code doesn't know that the value is already a timestamp. I've updated my question with more info.
    – user212218
    Commented Mar 2, 2012 at 22:17
  • You might be using someone's library which calls strtotime on what you supply, and you have a timestamp. Commented Apr 17, 2014 at 17:54
1

strtotime() outputs timestamp, and its parameter should by text representation of date, so you are using it wrong.

1

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

0

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.

2
  • "@1215282385" is actually a valid format. Take a look at the link in the comments.
    – Josh
    Commented Mar 2, 2012 at 22:06
  • Unfortunately, I have to -1 this, as the presence of @ has nothing to do with suppressing an error in this case; it's just part of PHP's documented format for unix timestamps.
    – user212218
    Commented Mar 2, 2012 at 22:27
0

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?

2
  • WRT why call strtotime() on a timestamp, the motivation behind this question is situations where the code doesn't know that the value is already a timestamp. I've updated my question with more info.
    – user212218
    Commented Mar 2, 2012 at 22:17
  • Unfortunately, I have to -1 this, as the presence of @ has nothing to do with suppressing an error in this case; it's just part of PHP's documented format for unix timestamps.
    – user212218
    Commented Mar 2, 2012 at 22:29
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.