Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

date_format()

Example

Create a new DateTime object and format the date.

<?php
$date=date_create(“2013-03-15”);
echo date_format($date,“Y/m/d H:i:s”);
?>

Definition and Usage

The date_format() function returns a date formatted according to the specified format.

Note: This function does not use locales; all output is in English.

 

Tip: You may also want to check the date() function, which formats dates and times based on the local settings.

Syntax

date_format(object, format)

Parameter Values

 

Parameter

Description

object

Required. Specifies a DateTime object created by date_create().

format

Required. Specifies the format for the date using the following characters:

  • d – Day of the month (01 to 31)
  • D – Abbreviated weekday name (e.g., Mon, Tue)
  • j – Day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – Full weekday name (e.g., Monday, Tuesday)
  • N – ISO-8601 numeric day representation (1 for Monday, 7 for Sunday)
  • S – English ordinal suffix for the day of the month (e.g., st, nd, rd, th; works with j)
  • w – Numeric day of the week (0 for Sunday, 6 for Saturday)
  • z – Day of the year (0 to 365)
  • W – ISO-8601 week number of the year (weeks starting on Monday)
  • F – Full month name (e.g., January, February)
  • m – Numeric month representation (01 to 12)
  • M – Abbreviated month name (e.g., Jan, Feb)
  • n – Numeric month representation without leading zeros (1 to 12)
  • t – Number of days in the given month
  • L – Leap year indicator (1 if leap year, 0 otherwise)
  • o – ISO-8601 year number
  • Y – Four-digit year representation
  • y – Two-digit year representation
  • a – Lowercase am/pm
  • A – Uppercase AM/PM
  • B – Swatch Internet time (000 to 999)
  • g – 12-hour format of hour without leading zeros (1 to 12)
  • G – 24-hour format of hour without leading zeros (0 to 23)
  • h – 12-hour format of hour with leading zeros (01 to 12)
  • H – 24-hour format of hour with leading zeros (00 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds with leading zeros (00 to 59)
  • u – Microseconds (added in PHP 5.2.2)
  • e – Timezone identifier (e.g., UTC, GMT, Atlantic/Azores)
  • I (capital i) – Daylight Saving Time indicator (1 if DST, 0 otherwise)
  • O – Difference to GMT in hours (e.g., +0100)
  • P – Difference to GMT in hours and minutes (added in PHP 5.1.3)
  • T – Timezone abbreviation (e.g., EST, MDT)
  • Z – Timezone offset in seconds (e.g., -43200 to 50400)
  • c – ISO-8601 date (e.g., 2013-05-05T16:34:42+00:00)
  • r – RFC 2822 formatted date (e.g., Fri, 12 Apr 2013 12:01:05 +0200)
  • U – Seconds since the Unix Epoch (January 1, 1970 00:00:00 GMT)

Additionally, the following predefined constants (available since PHP 5.1.0) can be used:

  • DATE_ATOM – Atom format (e.g., 2013-04-12T15:52:01+00:00)
  • DATE_COOKIE – HTTP Cookies format (e.g., Friday, 12-Apr-13 15:52:01 UTC)
  • DATE_ISO8601 – ISO-8601 format (e.g., 2013-04-12T15:52:01+0000)
  • DATE_RFC822 – RFC 822 format (e.g., Fri, 12 Apr 13 15:52:01 +0000)
  • DATE_RFC850 – RFC 850 format (e.g., Friday, 12-Apr-13 15:52:01 UTC)
  • DATE_RFC1036 – RFC 1036 format (e.g., Fri, 12 Apr 13 15:52:01 +0000)
  • DATE_RFC1123 – RFC 1123 format (e.g., Fri, 12 Apr 2013 15:52:01 +0000)
  • DATE_RFC2822 – RFC 2822 format (e.g., Fri, 12 Apr 2013 15:52:01 +0000)
  • DATE_RFC3339 – Same as DATE_ATOM (since PHP 5.1.3)
  • DATE_RSS – RSS format (e.g., Fri, 12 Aug 2013 15:52:01 +0000)
  • DATE_W3C – World Wide Web Consortium format (e.g., 2013-04-12T15:52:01+00:00)

Technical Details

Return Value:

Returns the formatted date as a string; FALSE on failure.

PHP Version:

5.2+