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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
The above is the detailed content of PHP strtotime. For more information, please follow other related articles on the PHP Chinese website!

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
