PHP date format

by Yogi P - September 17, 2023

To format dates in PHP, you can use the date() function, which takes a format string and a timestamp as arguments and returns a string representing the formatted date.

Let us take an example and see how to use this function to format a date:

$timestamp = time();

$formattedDate = date("Y-m-d H:i:s", $timestamp);

// Output: 2022-12-15 14:01:42

echo $formattedDate;

In this example, the date() function is used to format the current time (which is returned by the time() function) as a string in the format “YYYY-MM-DD HH:MM:SS”. The format string is specified as the first argument to the date() function, and the timestamp is provided as the second argument.

The date() function supports a wide range of format specifiers that can be used to customize the formatting of the date string. For example, the following table shows some common format specifiers and their meanings:

Format Specifier Meaning Example Output

  • Y Year (4 digits) 2022
  • y Year (2 digits) 22
  • m Month (numeric, with leading zeros) 02
  • n Month (numeric, without leading zeros) 12
  • M Month (short name) Dec
  • F Month (long name) December
  • d Day (numeric, with leading zeros) 05
  • j Day (numeric, without leading zeros) 15
  • D Day (short name) Fri
  • l Day (long name) Friday
  • H Hour (24-hour format, with leading zeros) 14
  • G Hour (24-hour format, without leading zeros) 14
  • h Hour (12-hour format, with leading zeros) 02
  • g Hour (12-hour format, without leading zeros) 2
  • i Minute (with leading zeros) 01
  • s Second (with leading zeros) 42

You can use any combination of these format specifiers to create a custom date format string. For example, the following code uses a format string that includes several of these specifiers to create a more detailed date and time format:

$timestamp = time();

$yuFormatedDate = date("l, F j, Y - g:i:s A", $timestamp);

// Output: Sunday, September 175, 2023 - 2:01:42 PM

echo $yuFormatedDate;

This code produces a date and time string that includes the day of the week, the full month name, the day of the month, the year, and the time in a 12-hour format with AM/PM.


FAQs of PHP date format


Q: What is the date() function?

A: The date() function is a PHP function that formats a date and time. It takes a timestamp and a format string as arguments and returns a string representing the formatted date.

Q: How do I use the date() function?

A: To use the date() function, you first need to get a timestamp. You can do this using the time() function. Once you have a timestamp, you can then call the date() function with the timestamp and the format string as arguments.

Q: What is a format string?

A: A format string is a string that tells the date() function how to format the date. The format string can include any combination of format specifiers, which are characters that represent different parts of the date and time.

Q: What are some common format specifiers?

A: Some common format specifiers include:

  • Y: Year (4 digits)
  • y: Year (2 digits)
  • m: Month (numeric, with leading zeros)
  • n: Month (numeric, without leading zeros)
  • M: Month (short name)
  • F: Month (long name)
  • d: Day (numeric, with leading zeros)
  • j: Day (numeric, without leading zeros)
  • D: Day (short name)
  • l: Day (long name)
  • H: Hour (24-hour format, with leading zeros)
  • G: Hour (24-hour format, without leading zeros)
  • h: Hour (12-hour format, with leading zeros)
  • g: Hour (12-hour format, without leading zeros)
  • i: Minute (with leading zeros)
  • s: Second (with leading zeros)

Q: How can I create a custom date format?

A: To create a custom date format, you can use any combination of format specifiers. For example, the following code creates a custom date format that includes the day of the week, the full month name, the day of the month, the year, and the time in a 12-hour format with AM/PM:

$timestamp = time();

$FormatedDate = date(“l, F j, Y – g:i:s A”, $timestamp);

echo $FormatedDate;

This code produces a date and time string that looks like this:

Friday, December 15, 2022 – 2:01:42 PM

Q: Where can I learn more about PHP date formatting?

A: There are many resources available online that can teach you more about PHP date formatting. You can find tutorials, articles, and documentation on the PHP website.


Take this QUIZ and test your Knowledge on PHP date format
Which PHP function is used to format dates?
Share on: Share YogiRaj B.Ed Study Notes on twitter Share YogiRaj B.Ed Study Notes on facebook Share YogiRaj B.Ed Study Notes on WhatsApp
Latest Posts

CDMA Full Form

April 19, 2024

Table of 14

April 11, 2024

Tables 11 to 20

March 11, 2024

Tense Chart

December 22, 2023

Table of 13

December 20, 2023
Search this Blog
Categories