Updated April 5, 2023
Introduction to PHP strtotime
There are various functions in the PHP programming language to deal with the date and date related tasks, strtotime() is one of them. There are various uses of the date in the PHP language. The function strtotime() is a built-in function of the PHP programming language. This function takes two parameters out of which one is the required one and one is optional one. We can pass the string as a required parameter to play with the date as per our business requirements.
Syntax:
There are various ways in which we can use the strtotime() functions.
strtotime(DateTime, Now);
- The first parameter (DateTime) is all about the date/time in the string format. This parameter is the required one and we can’t skip while working with this function.
- The second parameter (Now) is an optional one.
- PHP support this function from very earlier from version 4+. Till now, various extensions have been made in this function.
How does PHP strtotime works?
This function is one of the basic functions in the PHP language. There are no additional plugins or the extensions are required before using this function. We can simply follow the syntax of this function and can start using it as per our business requirements.
For Example: If someone wants to know the current date (means which date is going on today) we can use this function. For this, first, we have to get the current timestamp then we can use the other date function to get the exact date from that timestamp.
Code:
<?php
$currentTimeStamp =strtotime("now");
echo "The output at the time of writing this article was: " .$currentTimeStamp;
?>
This line of code will give us the current timestamp.
Output:
Now, we will use another function to get the human-readable date from this timestamp.
Code:
<?php
$current_date = date('m/d/Y', $currentTimeStamp);
echo "\n Today is: " . $current_date;
?>
Output:
Examples of PHP strtotime
Given below are the examples mentioned:
Example #1
Here we begin with the combing above code as a program and see the output.
Code
<?php
$currentTimeStamp =strtotime("now"); // this line will gives us the current timestamp
echo "The current timestamp is: ". $currentTimeStamp;
$current_date = date('m/d/Y', $currentTimeStamp); // converting the timestamp into the readable format
echo "\n Today is: " . $current_date;
?>
Output:
Please note that, this output was at the time of writing and running this program. We will see the different output as per the current date.
Example #2
Change the dates to the timestamp. As we know changing the string date to the timestamp is the main function of the strtotime() function.
Code:
<?php
$date = '27/02/2010';
echo "Actual Date: ". $date."\n";
$date = str_replace('/', '-', $date);
// The nature of the strtotime() function is it takes the string date as a parameter in a specified format only.
// So we are converting the / with the - before passing it to this function we are talking about.
echo "After changing to format of the specified Date: ". $date."\n";
// Now changing the date again to the human-readable form with the different format...
echo "This is Date: ". date('Y-m-d', strtotime($date));
?>
Output:
Example #3
Changing the string date to the actual date.
Code:
<?php
echo strtotime("today") . "\n"; // this will give us the timestamp of today
echo strtotime("27 Mar 2020") . "\n"; // this will give us the timestamp of 27 Mar 2020
echo strtotime("+2 hours") . "\n"; // Timestamp of 2 hours later from now
echo strtotime("2 hours") . "\n"; // This will also give the Timestamp of 2 hours later from now
echo strtotime("-2 hours") . "\n"; // This will give the Timestamp of 2 hours before from now
echo strtotime("3 week") . "\n"; // Timestamp of 3 weeks later from now
echo strtotime("last Monday") . "\n" ; // This will give the Timestamp of the last Monday
echo strtotime("next Monday"); // This will give the Timestamp of the coming Monday
?>
Output at the time of running the above code, you will see the different outputs. It totally depends on when we run this program.
Output:
Example #4
Modify the above program bit more to the human-readable date along with these timestamps. Also, I am setting the current time zone to Asia/Kolkata so that we can get the date as per the India perspective. We can set the time zone to any zone as per the requirements.
Code:
<?php
date_default_timezone_set('Asia/Kolkata');
echo strtotime("today") ;
echo " Today is - " .date('Y-m-d H:i:s', strtotime("today"))."\n";
echo strtotime("27 Mar 2020") ;
echo " Today is - " .date('Y-m-d H:i:s', strtotime("27 Mar 2020"))."\n";
echo strtotime("+2 hours") ;
echo " After 2 hours from now - " .date('Y-m-d H:i:s', strtotime("+2 hours"))."\n";
echo strtotime("-2 hours") ;
echo " Before 2 hours from now - " .date('Y-m-d H:i:s', strtotime("-2 hours"))."\n";
echo strtotime("last Monday") ;
echo " Date and Time of Last Monday " .date('Y-m-d H:i:s', strtotime("last Monday"))."\n";
echo strtotime("next Monday"); // This will give the Timestamp of the coming Monday
echo " Date and Time of coming Monday " . date('Y-m-d H:i:s', strtotime("next Monday"))."\n";
?>
Output:
Conclusion
We can use this strtotime() PHP built-in function whenever required. The developer or coder must be aware of the specified string date as a parameter. We can use this function whenever we need to change a string date to the Unix Timestamp.
Recommended Articles
This is a guide to PHP strtotime. Here we discuss the introduction, syntax, and working of PHP strtotine along with different examples and code implementation. You may also have a look at the following articles to learn more –