PHP contains several inbuilt functions for changing the date time format. In this tutorial, we are going to see the characters used in representing date and the PHP functions used to change date formats. There are several characters used in PHP date formatting. Let us see some of the frequently used characters as follows.
|d and j||returns day of the month in numbers. d returns two digit numbers. Eg: 7 as 07.|
|z||returns day of the year.|
|N and w||returns day of the week. N takes Monday as the start of the week, whereas w takes Sunday.|
|D||returns Weekday abbreviation. Eg: Sun, Tue.|
|l||returns Weekday full name.|
|m and n||returns month in numbers(m returns two digit numbers).|
|t||returns a number of days in a month.|
|M||returns month abbreviation. Eg: Jan, Feb|
|F||returns month full name|
|y||returns year in two digits.|
|Y||returns year in four digits.|
|L||returns 1 if leap year, 0 or else.|
PHP contains in-built date functions to format given date or time. These are,
This function accepts two optional arguments. i.e. format and timestamp. The code shows an example for the PHP date() function.
// returns current date in dd/mm/YYYY format echo date("d-m-Y"); // returns date for timestamp, in dd/mm/YYYY format echo date("d-m-Y",time());
date_format() expects two argument, DateTime object and date format. The following PHP example shows how to create DateTime object from a date and use this object in date_format() function.
$date=date_create("3-6-2007"); // returns date as 3rd June, 2007 echo date_format($date,"jS F, Y")
This PHP code tutorial was published on June 5, 2013.