Formatting Dates in PHP

by Vincy. Last modified on July 2nd, 2022.

PHP contains several inbuilt functions for changing the date-time format. In this tutorial, we are going to see the characters used in representing data 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.

Date Format Characters

Character Description
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 Date Formatting Functions

PHP contains in-built date functions to format given date or time. These are,

  • date()
  • date_format()


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 a DateTime object from a date and use this object in the date_format() function.

$date = date_create("3-6-2007");
// returns date as 3rd June, 2007
echo date_format($date, "jS F, Y");
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page