In PHP programming, date formatting is a frequently required operation, and is generally completed using the date() function. The date() function can format the date according to the external input parameters, and can also operate the timestamp and return the formatted date string. Use the date() function to conveniently format and output the desired date format.
The following are some commonly used methods for formatting dates with the date() function:
Output date (month-day-year) format
echo date('m-d-Y'); // 例如:09-21-2021
Output date (day-month-year) format
echo date('d-m-Y'); // 例如:21-09-2021
Output date (year-month-day) format
echo date('Y-m-d'); // 例如:2021-09-21
Output date (week) format
echo date('l'); // 例如:Tuesday
Output date (two-digit year) format
echo date('y'); // 例如:21
Output date (month) format
echo date('F'); // 例如:September
Output date (month, abbreviation) format
echo date('M'); // 例如:Sep
Output date (timestamp) format
echo date('U'); // 返回当前的时间戳
Output date (with AM/PM) format
echo date('h:i:s a'); // 例如:03:35:22 pm
Summary: Use the date() function to format the date. You can output the date string at will according to the required format. . At the same time, you can master the above commonly used date formatting methods, which allows developers to complete date processing faster and more conveniently.
The above is the detailed content of How to use date function to format date in PHP. For more information, please follow other related articles on the PHP Chinese website!