You are reading the article Guide To How Does Php Strtotime Works With Examples updated in September 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Guide To How Does Php Strtotime Works With Examples
Introduction to PHP strtotimeThere 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
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 strtotimeGiven below are the examples mentioned:
Example #1Here we begin with the combing above code as a program and see the output.
<?php $currentTimeStamp =strtotime("now"); echo "The current timestamp is: ". $currentTimeStamp; $current_date = date('m/d/Y', $currentTimeStamp); 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 #2Change 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); echo "After changing to format of the specified Date: ". $date."n"; echo "This is Date: ". date('Y-m-d', strtotime($date));Output:
Example #3Changing the string date to the actual date.
<?php echo strtotime("today") . "n"; echo strtotime("27 Mar 2023") . "n"; echo strtotime("+2 hours") . "n"; echo strtotime("2 hours") . "n"; echo strtotime("-2 hours") . "n"; echo strtotime("3 week") . "n"; echo strtotime("last Monday") . "n" ; echo strtotime("next 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 #4Modify 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 2023") ; echo " Today is - " .date('Y-m-d H:i:s', strtotime("27 Mar 2023"))."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"); echo " Date and Time of coming Monday " . date('Y-m-d H:i:s', strtotime("next Monday"))."n";Output:
ConclusionWe 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 –
You're reading Guide To How Does Php Strtotime Works With Examples
Update the detailed information about Guide To How Does Php Strtotime Works With Examples on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!