How to Use strtotime() in PHP

Probably one of the most useful time/date related functions in PHP is strtotime(). In this lesson I will show you some of the basic uses for this incredibly useful function.

Usng the function is quite straightforward as we can pass in a multitude of strings and the function will parse each of them.

For example:


echo strtotime("now");
echo "<br>";
echo strtotime("today");
echo "<br>";
echo strtotime("last Tuesday");
echo "<br>";
echo strtotime("+1 year");

The function returns a Unix timestamp, which we can then operate on further. The output from the above code is:


We can use other functions on the Unix output and turn this into full dates as follows:


echo date('Y-m-d',strtotime('last Tuesday'));

I find this function incredibly useful when dealing with date and time database entries.

If you would like to learn more about PHP then enrol in the PHP for Beginners course.

Want to Learn More?

Become a member and get access to the member only area of this site here.


Your email address will not be published.

PHP for Beginners

Enrol in the full course here


Coming soon, a full course on using PHP, MySQL and PDO. Be notified as soon as the course goes live.