What is the method for formatting dates in PHP?

In PHP, the date() function can be used to format dates.

The usage is as follows:
date(format, timestamp)

The format parameter is required to specify the format of the date. Common formatting options include:

  1. Y: A four-digit year
  2. m: Month in two digits (01-12)
  3. d: Dates in double digits (01-31)
  4. H: The hour number in a 24-hour clock (00-23)
  5. i: Minutes in double digits (00-59)
  6. s: two-digit seconds (00-59)

The timestamp parameter is optional and represents the date to be formatted, defaults to the current time.

For example, to obtain the year and month of the current date, you can use the following code:

$date = date("Y-m");
echo $date;

The output would look something like: 2022-03

For more options and usage of formatting dates, please refer to the official PHP documentation at: https://www.php.net/manual/en/function.date.php

bannerAds