


Summary of usage of date and time function date() in PHP_PHP tutorial
date() is a commonly used date and time function. Let me summarize the various forms of usage of the date() function. Friends who need to learn can refer to it.
Format Date
The first parameter of the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some available
:
•d - day of the month (01-31)
•m - Current month as a number (01-12)
•Y - Current year (four digits)
You can find all the letters that can be used in the format parameter in our PHP Date reference manual.
You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formatting:
The code is as follows | Copy code | ||||
echo " ";
echo date("Y.m.d");
?> |
代码如下 | 复制代码 |
echo date('Y-m-j'); echo date('y-n-j'); |
2006/07/11
2006.07.11
代码如下 | 复制代码 |
echo date('Y-M-j'); echo date('Y-m-d'); |
1. Year-month-day
The code is as follows | Copy code | ||||
2007-02-6
07-2-6 |
Capital Y represents the four-digit year, while lowercase y represents the two-digit year;
Lowercase m represents the number of the month (with a leading), while lowercase n represents the number of the month without the leading.
The code is as follows | Copy code | ||||
echo date('Y-M-j'); echo date('Y-m-d'); 2007-02-06
|
The code is as follows | Copy code |
echo date('Y-M-j'); 2007-Feb-6 echo date('Y-F-jS'); 2007-February-6th |
The code is as follows | Copy code |
echo date('g:i:s a'); 5:56:57 am echo date('h:i:s A'); 05:56:57 AM |
A lowercase g indicates a 12-hour format without leading 0s, while a lowercase h indicates a 12-hour format with leading 0s.
When using the 12-hour clock, it is necessary to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".
The code is as follows | Copy code | ||||
14:02:26 |
Capital G represents the hour in 24-hour format, but without leading; use capital H to represent the hour in 24-hour format with leading
Summary:
Lowercase g and h represent the 12-hour format, while uppercase G and H represent the 24-hour format.
代码如下 | 复制代码 |
echo date('L'); |
代码如下 | 复制代码 |
echo date('l'); |
代码如下 | 复制代码 |
echo date('D'); |
Today is: Tue
Capital L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
代码如下 | 复制代码 |
echo date('w'); |
代码如下 | 复制代码 |
echo date('W'); |
This week is week 06 of the year
代码如下 | 复制代码 |
echo date('t'); |
This month is 28 days
代码如下 | 复制代码 |
echo date('z'); |
Today is the 36th day of the year
Lowercase t represents the number of days in the current month
Lowercase z indicates what day of the year today is
4, other
The code is as follows | Copy code | ||||
echo date('T');
|
Capital T indicates the server’s time locale
The code is as follows | Copy code | ||||
echo date('I');
|
Capital I means to determine whether the current daylight saving time is, if true, return 1, otherwise 0
The code is as follows | Copy code | ||||
echo date('U');
|
Capital U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp of the Unix time epoch.
The code is as follows | Copy code | ||||
echo date('c');
|
Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, use the letter T to separate the date and time, the time format is HH:MM:SS, and the time zone uses Greenway
Expressed by the deviation from GMT.
The code is as follows | Copy code | ||||
echo date('r');
|
Lowercase r indicates RFC822 date.
Add timestamp
The second parameter of the date() function specifies a timestamp. This parameter is optional. If you do not provide a timestamp, the current time will be used.
In our example, we will use the mktime() function to create a timestamp for tomorrow.
The mktime() function returns a Unix timestamp for a specified date.
Grammar
mktime(hour,minute,second,month,day,year,is_dst) If we need to get the timestamp of a certain day, we only need to set the
day parameter will do:
The code is as follows | Copy code | ||||
|
The output of the above code is similar to this:
Tomorrow is 2006/07/12
There are also some more advanced date and time functions to introduce to you
This category will introduce more functions to enrich our applications.
The code is as follows | Copy code | ||||
|
This function can be used to check and validate a date before it is used in calculations or saved in the database.
代码如下 | 复制代码 |
// returns false getdate($ts) |
The code is as follows | Copy code |
// returns false getdate($ts) |
With no arguments, this function returns the current date and time in a combined array. Each element in the array represents one of the date/time values
代码如下 | 复制代码 |
mktime($hour, $minute, $second, $month, $day, $year) |
Apply this function to obtain a series of discrete, easily separable date/time values.
The code is as follows | Copy code | ||||
// get date as associative array $arr = getdate(); echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year']; echo "Time is " . $arr['hours'] . ":" . $arr['minutes']; ?> mktime($hour, $minute, $second, $month, $day, $year)
|
This function does the opposite of getdate(): it generates a UNIX time stamp from a series of date and time values (GMT time January 1, 1970 to
Number of seconds elapsed now). When no arguments are used, it generates a UNIX time stamp of the current time.
Use this function to obtain the UNIX time label of the immediate time. Such timestamps are commonly used in many databases and programming languages.
The code is as follows | Copy code | ||||
echo mktime(13,15,23,6,7,2006); ?> date($format, $ts) |
The code is as follows | Copy code |
// format current date // returns "13-Sep-2005 01:16 PM" echo date("d-M-Y h:i A", mktime()); ?> strtotime($str) |
This function converts a human-readable English date/time string into a UNIX time tag.
Apply this function to convert a non-standardized date/time string into a standard, compatible UNIX time tag.
The code is as follows | Copy code | ||||
echo date("d-M-y", strtotime("today")); // returns 14-Sep-05 echo date("d-M-y", strtotime("tomorrow")); // returns 16-Sep-05 echo date("d-M-y", strtotime("today +3 days")); ?> strftime($format,$ts)
|
代码如下 | 复制代码 |
// set locale to France (on Windows) // format month/day names echo strftime("Month: %B "); |
Apply this function to create a date string compatible with the current environment.
The code is as follows | Copy code |
// set locale to France (on Windows) // format month/day names echo strftime("Month: %B "); |
代码如下 | 复制代码 |
// run some code // get ending value // calculate time taken for code execution |
microtime()
As defined by the previous setlocale() function, this function formats the UNIX time stamp into a date string suitable for the current environment.
Apply this function to create a date string compatible with the current environment.
The code is as follows | Copy code | ||||
// get starting value $start = microtime(); // run some code for ($x=0; $x $null = $x * $x; }
// get ending value // calculate time taken for code execution |
代码如下 | 复制代码 |
|
The code is as follows | Copy code |
// returns timestamp for 12:25:23 9-Jul-2006 echo gmmktime(12,25,23,7,9,2006); ?> |
The code is as follows | Copy code |
// format current date into GMT // returns "13-Sep-2005 08:32 AM" echo gmdate("d-M-Y h:i A", mktime()); ?> |
date_default_timezone_set($tz), date_default_timezone_get()
All date/time function calls after this function set and restore the default time zone.
Note: This function is only valid in PHP 5.1+.
This function is a convenient shortcut for setting the time zone for future time operations.
The code is as follows | Copy code | ||||
// set timezone to UTC date_default_timezone_set('UTC'); ?> |

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

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.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
