Jan 27, 2020 4:00 PM

Date formats in PHP

Date formats in PHP

In this article I will present a table with the most common date formats in PHP. In the last column I indicated the countries in which these formats can be used. The specified date formats in PHP are used in the functions date() and gmdate(), which are designed to format the date/time.

Date format Date format in PHP Date example (7 Janiary 1970) Countries of use
yyyy-mm-dd Y-m-d 1970-01-07 Poland, Sweden, Lithuania, Canada, China (PRC), France, Germany, South Korea, Mongolia, Portugal, South Africa, Sri Lanka, Switzerland, Belgium
yyyy.mm.dd Y.m.d 1970.01.07 Hungary
mm/dd/yyyy m/d/Y 01/07/1970 Panama, Philippines
dd/mm/yyyy d/m/Y 07/01/1970 Great Britain, Vietnam, Israel, Indonesia, Spain, Italy, France, Somalia
dd-mm-yyyy d-m-Y 07-01-1970 Denmark, Portugal
mm-dd-yyyy m-d-Y 01-07-1970 USA
m/d/yy n/j/y 1/7/70 USA
m/d/yyyy n/j/Y 1/7/1970 USA
dd/m/yyyy d/n/Y 07/1/1970 Brazil, Greece, Thailand
yyyy/m/dd Y/n/d 1970/1/07 Hong Kong, Taiwan
yyyy/mm/dd Y/m/d 1970/01/07 Iran, Japan
dd-m-yyyy d-n-Y 07-1-1970 Netherlands
dd.mm.yyyy d.m.Y 07.01.1970 Bulgaria, Germany, Norway, Belgium, Romania, Russia, Slovenia, Slovakia, Turkey, Ukraine, Belarus
d.m.yyyy j.n.Y 7.1.1970 Finland, Czech Republic, Serbia
yyyy-m-d Y-n-j 1970-1-7 China (PRC)
yyyy mmmm dd Y F d 1970 Janiary 07 Rwanda
dd. mmmm yyyy d. F Y 07. Janiary 1970 Serbia
d. mmmm yyyy j. F Y 7. Janiary 1970 Great Britain
mmmm dd, yyyy F d, Y Janiary 07, 1970 Somalia
d mmm yyyy j M Y 7 Jan 1970 USA
d mmmm yyyy j F Y 7 Janiary 1970 USA

where:

Sometimes you may need to implement a date format choice for the user. Use this function, which returns an array of date formats, where key is the date format, value is the current date in this format:

function getDateFormats()
{
    return [
        'Y-m-d' => date('Y-m-d'),
        'Y.m.d' => date('Y.m.d'),
        'm/d/Y' => date('m/d/Y'),
        'm/d/y' => date('m/d/y'),
        'd/m/Y' => date('d/m/Y'),
        'd/m/y' => date('d/m/y'),
        'd-m-Y' => date('d-m-Y'),
        'd-m-y' => date('d-m-y'),
        'm-d-Y' => date('m-d-Y'),
        'm-d-y' => date('m-d-y'),
        'n/d/Y' => date('n/d/Y'),
        'n/d/y' => date('n/d/y'),
        'd/n/Y' => date('d/n/Y'),
        'd/n/y' => date('d/n/y'),
        'n-d-Y' => date('n-d-Y'),
        'n-d-y' => date('n-d-y'),
        'd-n-Y' => date('d-n-Y'),
        'd-n-y' => date('d-n-y'),
        'd.m.Y' => date('d.m.Y'),
        'd.m.y' => date('d.m.y'),
        'j.n.Y' => date('j.n.Y'),
        'j.n.y' => date('j.n.y'),
        'M d, Y' => date('M d, Y'),
        'F d, Y' => date('F d, Y'),
        'F j, Y' => date('F j, Y'),
        'M d Y' => date('M d Y'),
        'd M Y' => date('d M Y'),
        'd M y' => date('d M y'),
        'Y M d' => date('Y M d'),
    ];
}

The following result is expected after calling this function:

print_r(getDateFormats());

/**
[output]
array (
  'Y-m-d' => '2020-01-27',
  'y.m.d' => '2020.01.27',
  'm/d/Y' => '01/27/2020',
  'm/d/y' => '01/27/20',
  'd/m/Y' => '27/01/2020',
  'd/m/y' => '27/01/20',
  'd-m-Y' => '27-01-2020',
  'd-m-y' => '27-01-20',
  'm-d-Y' => '01-27-2020',
  'm-d-y' => '01-27-20',
  'n/d/Y' => '1/27/2020',
  'n/d/y' => '1/27/20',
  'd/n/Y' => '27/1/2020',
  'd/n/y' => '27/1/20',
  'n-d-Y' => '1-27-2020',
  'n-d-y' => '1-27-20',
  'd-n-Y' => '27-1-2020',
  'd-n-y' => '27-1-20',
  'd.m.Y' => '27.01.2020',
  'd.m.y' => '27.01.20',
  'j.n.Y' => '27.1.2020',
  'j.n.y' => '27.1.20',
  'M d, Y' => 'Jan 27, 2020',
  'F d, Y' => 'January 27, 2020',
  'F j, Y' => 'January 27, 2020',
  'M d Y' => 'Jan 27 2020',
  'd M Y' => '27 Jan 2020',
  'd M y' => '27 Jan 20',
  'Y M d' => '2020 Jan 27',
)
 */

Other articles