PHP | strtotime() Function

PHP strtotime() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP strtotime() Function.

The built-in strtotime() function accepts a date and time string, with textual data/time values and parses it as an Unix timestamp.

Syntax

Following below is the syntax to use this function -

strtotime($time)


Parameter Details

Sr.NoParameter & Description
1

time(Mandatory)

This value represents the date/time string.

2

now(Optional)

This represents a timestamp which is used as a base for the calculation of relative dates..


Return Value

This built-in PHP function returns a timestamp value for the given date string. In case of failure, this function returns the boolean value false.

PHP Version

This function was first introduced as part of core PHP version 4.0 and, it works with all of the later versions.

Example1

Following example illustrates the usage of strtotime() function -

<?php
   $str = strtotime("12 September 2009");
   print("Timestamp: ".$str); 
?>

Output

When the above code is executed, it will produce the following result -

Timestamp: 1252713600

Example2

If you pass now as a parameter, this function returns the current time stamp -

<?php
   $str = strtotime("now");
   print("Timestamp: ".$str); 
?>

Output

When the above code is executed, it will produce the following result -

Timestamp: 1606097306

Example3

Now let us invoke this function by passing various data values -

<?php
   print("Time stamp of 25 December 2019: ".strtotime("25 December 2019")."\n");
   print("Time stamp of +26 day: ".strtotime("+26 day")."\n");
   print("Time stamp of +3 week: ".strtotime("+3 week")."\n");
   print("Time stamp of +3 month 9 days 9 hours: ".strtotime("+3 month 9 days 9 hours")."\n");
   print("Time stamp of last Sunday: ".strtotime("last Sunday")."\n");
   print("Time stamp of next Friday: ".strtotime("next Friday")."\n");
?>

Output

When the above code is executed, it will produce the following result -

Time stamp of 25 December 2019: 1577232000 
Time stamp of +26 day: 1608343856 
Time stamp of +3 week: 1607911856 
Time stamp of +3 month 9 days 9 hours: 1614856256 
Time stamp of last Sunday: 1606003200 
Time stamp of next Friday: 1606435200

Example4

Try the following example -

<?php
   $timestamp = strtotime( "February 15, 2015" );   
   print date( 'Y-m-d', $timestamp );
?>

Output

When the above code is executed, it will produce the following result -

2015-02-15


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP time() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.