date() – Format a local time/date


**date() – Format a local time/date**

The `date()` function is a versatile function in PHP that is used to format a local time/date. It takes a timestamp as its first argument and a format string as its second argument. The format string specifies the format of the output.

Here are some of the most commonly used format specifiers:

* `Y` – Year, four digits
* `y` – Year, two digits
* `m` – Month, numeric (01-12)
* `M` – Month, textual (Jan-Dec)
* `d` – Day of the month, numeric (01-31)
* `D` – Day of the week, textual (Mon-Sun)
* `H` – Hour, 24-hour format (00-23)
* `h` – Hour, 12-hour format (01-12)
* `i` – Minutes (00-59)
* `s` – Seconds (00-59)
* `A` – Ante meridiem and post meridiem (AM or PM)

For example, the following code would output the current date and time in the format “Y-m-d H:i:s”:

“`php
echo date(‘Y-m-d H:i:s’);
“`

The `date()` function can also be used to format timestamps in the past or future. For example, the following code would output the date and time one week from now:

“`php
echo date(‘Y-m-d H:i:s’, strtotime(‘+1 week’));
“`

The `date()` function is a powerful tool that can be used to format dates and times in a variety of ways. It is a valuable function to have in your PHP toolkit.

**Here are some additional examples of how the `date()` function can be used:**

* Get the current year:

“`php
echo date(‘Y’);
“`

* Get the current month:

“`php
echo date(‘m’);
“`

* Get the current day of the month:

“`php
echo date(‘d’);
“`

* Get the current day of the week:

“`php
echo date(‘D’);
“`

* Get the current hour:

“`php
echo date(‘H’);
“`

* Get the current minute:

“`php
echo date(‘i’);
“`

* Get the current second:

“`php
echo date(‘s’);
“`

* Get the current time in the format “Y-m-d H:i:s”:

“`php
echo date(‘Y-m-d H:i:s’);
“`

* Get the date and time one week from now:

“`php
echo date(‘Y-m-d H:i:s’, strtotime(‘+1 week’));
“`