Home  >  Article  >  Backend Development  >  How to use date function to format date in PHP

How to use date function to format date in PHP

PHPz
PHPzOriginal
2023-06-26 17:21:592071browse

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:

  1. Output date (month-day-year) format

    echo date('m-d-Y'); // 例如:09-21-2021
  2. Output date (day-month-year) format

    echo date('d-m-Y'); // 例如:21-09-2021
  3. Output date (year-month-day) format

    echo date('Y-m-d'); // 例如:2021-09-21
  4. Output date (week) format

    echo date('l'); // 例如:Tuesday
  5. Output date (two-digit year) format

    echo date('y'); // 例如:21
  6. Output date (month) format

    echo date('F'); // 例如:September
  7. Output date (month, abbreviation) format

    echo date('M'); // 例如:Sep
  8. Output date (timestamp) format

    echo date('U'); // 返回当前的时间戳
  9. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn