php

PHP Strtotime() Function

The UNIX timestamp value is calculated from 1st January 1970. The strtotime() is a built-in PHP function to convert human-readable date and time values into UNIX timestamp values. The way to use this function in PHP for different purposes has shown in this tutorial with examples.

Syntax:

strtotime (dttime_string, [ts_now]):timestamp|False

The first argument of the function is mandatory and contains the string of valid date and time values. The second argument is optional that is used to set the base value for calculating the timestamp value. It returns the timestamp value based on the first argument value.

Pre-requisite

The scripts used in the examples of this tutorial are written based on the PHP 8 version. Do the following task before executing the examples of this script.

  1. Install apache2 and PHP 8.
  2. Set execute permission for all files and folders under /var/www/html folder where all PHP files will be stored.

Different Uses of Strtotime() Function

The timestamp values of the particular date and time can be calculated in different ways by using the strtotime() function. The uses of this function have shown in this part of this tutorial.

Example-1: Uses of the mandatory argument of strtotime() function

Create a PHP file with the following script to find out the particular date by calculating the timestamp value by using strtotime() function. Here, eight different string values of date and time have been used in this function to generate and print the timestamp value. Next, the human-readable date and time value will be printed based on the calculated timestamp value.

<?php

//Read current timestamp value
$today = strtotime('now');
echo "The timestamp value of today is: $today<br>";
echo "The date of today is: ".date("d-m-Y", $today)."</br></br>";

//Read the timestamp value of yesterday
$prev_day = strtotime('-1 day');
echo "The timestamp value of yesterday is: $prev_day<br>";
echo "The date of the yesterday is: ".date("d-m-Y", $prev_day)."</br></br>";

//Read the timestamp value of tomorrow
$next_day = strtotime('+1 day');
echo "The timestamp value of tomorrow is: $next_day<br>";
echo "The date of the tomorrow is: ".date("d-m-Y", $next_day)."</br></br>";

//Read the timestamp value of next thursday
$n_day = strtotime('next Thursday');
echo "The timestamp value of next Thursday is: $n_day<br>";
echo "The date of the next Thursday is: ".date("d-m-Y", $n_day)."</br></br>";

//Read the timestamp value of previous tuesday
$l_day = strtotime('last Tuesday');
echo "The timestamp value of last Tuesday is: $l_day<br>";
echo "The date of the last Tuesday is: ".date("d-m-Y", $l_day)."</br></br>";

//Read the timestamp value of future day
$future_date = strtotime('+5 years 5 months 5 days');
echo "The timestamp value after 5 years 5 months and 5 days is: $future_date<br>";
echo "The date after 5 years 5 months and 5 days is: ".date("d-m-Y", $future_date)."</br></br>";

//Read the timestamp value of previous hour
$prev_hour = strtotime('-10 hours 30 minutes 15 seconds');
echo "The timestamp value before 10 hours 30 minutes and 15 seconds is: $prev_hour<br>";
echo "The date before 10 hours 30 minutes and 15 seconds  is: ".date("d-m-Y", $prev_hour)."</br></br>";

//Read the timestamp value of the particular day
$specific_day = strtotime('16 December 2021');
echo "The timestamp value of 16th December 2021 is: $prev_hour<br>";

?>

Output:

The following output will appear after executing the above script. Here, the filename is strtotime1.php that is stored inside /var/www/html/code folder. The output of the script will be changed based on the date and time of the operating system.

http://localhost/code/strtotime1.php

Example-2: Calculate the timestamp value from the valid date format

Create a PHP file with the following script to check the date from the URL query parameter is valid and find out the timestamp value of that date. Here, the $_GET[] array has been used to read the value of the URL query parameters named ‘dt’. The provided date value given in the URL query parameter is valid or not will be checked by the preg_match() function. If the valid date is provided then the timestamp value of the date will be calculated and printed.

<?php

//Check the date value is given in the URL or not
if(isset($_GET['dt']))
{  
    //Read the date value
    $date = $_GET['dt'];

    //Check the date value is valid or not
    if(preg_match('/[0-9]{2}-[0-9]{2}-[0-9]{4}/', $date))
    {
        //Read the timestamp value of the date
        $tm = strtotime($date);
        //Print the timestamp value
        echo "<br /><h3>The timestamp value of $date is $tm.</h3>";
    }
    else
        //Print error message for invalid date format
        echo "<br /><h3>Invalid date format.</h3>";
}
else
    //Print error message if date is not found in the URL
    echo "<br /><h3>No date value found.</h3>";

?>

Output:

The following output will appear after executing the above script without any query parameter. Here, the filename is strtotime2.php that is stored inside /var/www/html/code folder.

http://localhost/code/strtotime2.php

The following output will appear after executing the above script with the query parameter named ‘dt’ with the value, ‘23-10-2021’.

http://localhost/code/strtotime2.php?dt=23-10-2021

The following output will appear after executing the above script with the query parameter named ‘dt’ with the value, ‘23-100-2021’.

http://localhost/code/strtotime2.php?dt=23-100-2021

Example-3: Calculate the difference between two dates

Create a PHP file with the following script to find out the difference between two dates by using the strtotime() function. Here, two valid date values have been assigned in two variables. Two timestamp values of these dates have been calculated by using the strtotime() function. The difference of these timestamp values has been calculated in seconds that have been converted into the years. The converted value will be printed after executing the code.

<?php

    //Set two date values
    $date1 = '01-01-2021';
    $date2 = '01-01-2024';

    //Calculate the difference between the date
    $diff = (strtotime($date2) - strtotime($date1))/60/60/24/365;

    //Print the difference value in years
    echo "<br /><h3>The difference between two dates is $diff years</h3>";

?>

Output:

The following output will appear after executing the above script without any query parameter. Here, the filename is strtotime3.php that is stored inside /var/www/html/code folder. The difference between ’01-01-2021′ and ’01-01-2024′ is 3 years that have been printed in the output.

http://localhost/code/strtotime3.php

Conclusion

The strtotime() is a very useful function for working with the date and time values. The ways to use this function for different purposes have been described in this tutorial to help the new PHP user to know the purpose of using this function in PHP script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.