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

How to use date function to format date in PHP

PHPz
Release: 2023-06-26 17:24:01
Original
2132 people have browsed it

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
    Copy after login
  2. Output date (day-month-year) format

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

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

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

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

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

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

    echo date('U'); // 返回当前的时间戳
    Copy after login
  9. Output date (with AM/PM) format

    echo date('h:i:s a'); // 例如:03:35:22 pm
    Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template