• PHP Video Tutorials

PHP date_format() Function



Definition and Usage

This function is an alias of DateTime::format() function. It accepts a DateTime object and a format string (representing a desired date/time format) as parameters, formats the object in the specified format and, returns the result.

Syntax

date_format($date_time_object, $format)

Parameters

Sr.No Parameter & Description
1

date_time_object(Mandatory)

This is the DateTime object which you need to format.

2

format(Mandatory)

This is the string representing the desired format.

Return Values

PHP date_format() function returns the formatted date string

PHP Version

This function was first introduced in PHP Version 5.2.1 and works in all the later versions.

Example

Try out following example in here, we are creating a DateTime object and formatting it −

Live Demo
<?php
   //Creating a DateTime object
   $date_time_Obj = date_create("25-09-1989");
   //formatting the date/time object
   $format = date_format($date_time_Obj, "y-d-m");
   print("Date in yy-dd-mm format: ".$format); 
?>

This will produce following result −

Date in yy-dd-mm format: 89-25-09

Example

Following example formats a DateTime object as date and time separately −
<?php
   $dateString = '11-06-2012 12:50:41 GMT';
   $dateTimeObj = date_create($dateString);
   $date = date_format($dateTimeObj, 'd-m-y');
   print("Date: ".$date); 
   print("\n");
   $time = date_format($dateTimeObj, 'H:i:s');
   print("Time: ".$time); 
?>

This will produce following result −

Date: 11-06-12
Time: 12:50:41

Example

Following example demonstrates few valid format strings in PHP −

<?php
   $dateTimeObj = date_create("11-06-2012 12:50:41 GMT");
   print("Date in y-m-d format: ".date_format($dateTimeObj, 'Y-m-d'));
   print("\n");
   print("Date in d/m/y format: ".date_format($dateTimeObj, 'd/m/y'));
   print("\n");
   print("Date in Y-m-d H:i:s format: ".date_format($dateTimeObj, 'Y-m-d H:i:s'));
   print("\n");
   print("Date in G:i:A format: ".date_format($dateTimeObj, 'G-i-A'));
?>

This will produce following result −

Date in y-m-d format: 2012-06-11
Date in d/m/y format: 11/06/12
Date in Y-m-d H:i:s format: 2012-06-11 12:50:41
Date in G:i:A format: 12-50-PM

Example

Following example creates a new date using the date_format() method −

<?php
   $dateSrc = '2005-04-19 12:50 GMT';
   $dateTime = date_create( $dateSrc);;
   # Now set a new date using date_format();
   date_format( $dateTime, 2000, 12, 12);
   
   echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
   echo "
"; # Using second function. $dateTime = new DateTime($dateSrc); $dateTime->setDate( 1999, 10, 12); echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z"); ?>

This produces the following output −

New Formatted date is 2000-12-12T12:50:00Z
New Formatted date is 1999-10-12T12:50:00Z

Format String

There are certain characters with predefined meaning using which you can create a format string They are:

  • d − The day of the month (from 01 to 31)

  • D − A textual representation of a day (three letters)

  • j − The day of the month without leading zeros (1 to 31)

  • l (lowercase 'L') − A full textual representation of a day

  • N − The ISO-8601 numeric representation of a day (1 for Monday through 7 for Sunday)

  • S − The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)

  • w − A numeric representation of the day (0 for Sunday through 6 for Saturday)

  • z − The day of the year (from 0 through 365)

  • W − The ISO-8601 week number of year (weeks starting on Monday)

  • F − A full textual representation of a month (January through December)

  • m − A numeric representation of a month (from 01 to 12)

  • M − A short textual representation of a month (three letters)

  • n − A numeric representation of a month, without leading zeros (1 to 12)

  • t − The number of days in the given month

  • L − Whether it's a leap year (1 if it is a leap year, 0 otherwise)

  • o − The ISO-8601 year number

  • Y − A four digit representation of a year

  • y − A two digit representation of a year

  • a − Lowercase am or pm

  • A − Uppercase AM or PM

  • B − Swatch Internet time (000 to 999)

  • g − 12-hour format of an hour (1 to 12)

  • G − 24-hour format of an hour (0 to 23)

  • h − 12-hour format of an hour (01 to 12)

  • H − 24-hour format of an hour (00 to 23)

  • i − Minutes with leading zeros (00 to 59)

  • s − Seconds, with leading zeros (00 to 59)

  • e − The timezone identifier (Examples: UTC, Atlantic/Azores)

  • I (capital i) − Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)

  • O − Difference to Greenwich time (GMT) in hours (Example: +0100)

  • T − Timezone setting of the PHP machine (Examples: EST, MDT)

  • Z − Timezone offset in seconds. The offset west of UTC is negative, and the offset east of UTC is positive (-43200 to 43200)

  • c − The ISO-8601 date (e.g. 2004-02-12T15:19:21+00:00)

  • r − The RFC 2822 formatted date (e.g. Thu, 21 Dec 2000 16:01:07 +0200)

  • U − The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

php_function_reference.htm
Advertisements