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:

<?php

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:

1515565199
1515560400
1515474000
1547101199

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

<?php

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.

LEARN MORE

Comments

Your email address will not be published.

PHP for Beginners

Would you like lifetime access to the hugely successful PHP for Beginners course? Click here to find out how.

FREE Code Snippets

FREE hints and tips delivered direct to you once per month.