Home » Programming » PHP » Php Strtotime

PHP strtotime() Function – Converting Text to Timestamp [With Examples]

The PHP strtotime() function takes a string containing text and converts it to a Unix Timestamp. Find out how to use this function in this guide.

Unix timestamp is a number representing the number of seconds since 00:00:00 UTC on 1 January 1970. From it, you can build more complex Date/Time objects which can readily provide Dates/Minutes/Hours/Seconds or other information about the supplied time.

strtotime Syntax

strtotime ($datetime , $baseTimestamp)

Note that:

  • $datetime is your supplied text string (possibly) containing a date
    • strtotime() supports the English language only
    • See here for a comprehensive list of compatible formats
    • ..or see below for some real-world examples
  • $baseTimestamp is the Unix timestamp used as a base which relative dates are calculated from
    • It is optional
  • strtotime() will return a Unix timestamp (a number) if it succeeds in parsing your input or FALSE if it fails
    • No timezone information is included in the returned Date object
    • You should add some by converting the timestamp into a DateTime object and adding the timezone data as applicable
  • Any date or time supplied to strtotime() is assumed to be in the system default timezone as configured in PHP

strtotime Examples

Here’s a few examples of using strtotime():

echo strtotime("now"); # Outputs the current Unix timestamp

echo strtotime("23 December 2007"); # Unix timestamp for a given date

Relative Times

You can also get the time for a date relative to the current:

echo strtotime("+1 day"); # Outputs the Unix timestamp for todays date, plus a day
echo strtotime("+1 week 3 days 2 hours 1 seconds"); # Multiple units can be supplied
echo strtotime("next Tuesday"); # Textual descriptors are supported - see the comprehensive list above for which words will work
echo strtotime("last Wednesday");

Converting to a Date object with Timezone Info

$date = new DateTime(); # Create a new DateTime object - it will default to the current time
$date->setTimestamp(strtotime("next Tuesday")); # Set the time via strtotime
$date->setTimezone(new DateTimeZone('Australia/Sydney')); # Add timezone information
print_r($date); # $date is a DateTime object, not a string, so it can't just be echo'd - print_r prints the object properties recursively

Conclusion

Converting text representations of times to something a computer can run calculations on is a common task. It’s used when reading and calculating dates from a database or file and is often used for checking a user’s age from their birthday or checking whether a database record falls into a specific date range.

For more examples, check out the official PHP strtotime() documentation at:

https://www.php.net/manual/en/function.strtotime.php

…and be sure to check out our other PHP articles!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment