7

PHP has strtotime() which assumes the input given is a string that represents a valid time. Before using strtotime(), how can I verify that the string about to be given to it is in one of its valid formats?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
zmol
  • 2,834
  • 5
  • 26
  • 29
  • 3
    Why do you need to know this in advance? Can't you just check if strtotime() returns FALSE, and then react accordingly? – Per H Feb 11 '11 at 09:06
  • @Per This is valid `strtotime('last monday')`. But what about this `strtotime('first monday of last month')`? And this `strtotime('first day of this month last year')`? Useful to be able to quickly test this. – Tamlyn Jul 16 '14 at 14:04

2 Answers2

12

strtotime() returns false if it can't understand your date format, so you can check its return value. The function doesn't have any side effects, so trying to call it won't hurt.

// $format = ...

if (($timestamp = strtotime($format)) !== false) {
    // Obtained a valid $timestamp; $format is valid
} else {
    // $format is invalid
}

Be warned, however, that strtotime() only supports the same range of Unix timestamps as the server architecture does. This means it will incorrectly return false for certain date ranges. From the manual:

Note:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

Because server architectures can vary, you may be limited to 32-bit Unix timestamps depending on your use case, even though 64-bit timestamps cover a practically infinite period of time.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • -1 for this answer. strtotime will also return false for dates before 1901 or after 2038, even if the format is valid. – marcvangend May 24 '11 at 12:26
  • That comment took long. And really, does that actually matter? – BoltClock May 24 '11 at 12:27
  • 2
    Yes, it does matter. Not always, I admit; if you can be 100% sure that the dates will be within the 1901-2038 range, you're safe. The thing is, you can never be sure how your code will be (re)used a couple of years from now. The Y2K problems have made that very clear and I think programmers should be aware of those limitations. – marcvangend May 24 '11 at 12:32
  • 1
    won't work. you can try strtotime('2004') it's will return 20:04 today :)) – TomSawyer Apr 21 '13 at 02:12
  • @TomSawyer: And how is that not a valid time? – BoltClock Apr 21 '13 at 03:03
  • @BoltClock 2004 is a year or hour?. that's can make we confuse – TomSawyer Apr 22 '13 at 09:11
  • @TomSawyer: Well, clearly `strtotime()` thinks it's a valid 24-hour time, which it is. How does that not answer the question? – BoltClock Apr 22 '13 at 09:18
  • @marcvangend: I re-read the comments you posted a while back, and have finally updated my answer with a warning regarding this limitation. That's the best I can do since my answer has already been accepted. – BoltClock Apr 22 '13 at 09:32
  • 1
    @BoltClock Fair enough, I'll turn my -1 into a +1 then. Thanks. – marcvangend Apr 22 '13 at 10:43
3

Actually you can't check that before executing the function, but you could take a look at the official php.net page. The only thing you can do is checking whether your function returns "false".

Here's a link strtotime().

There are a lot of examples of what you can do with strtotime().

oopbase
  • 11,157
  • 12
  • 40
  • 59