AMSOL e-Tutor

AMSOL e-Tutor helps you learn web programming better with examples. It also gives sample codes to implement.

PHP Date


Date is a built in PHP function, it handles all date time related functionalities. It can get us current date or time, any day further or backwards. To display date below is the simplest example.

echo date("d-m-Y");

Date can be displayed in any format just changing parameters, below are few examples

echo date("d-m-Y"); 28-08-2020
echo date("M-d-Y"); Aug-28-2020
echo date("H:i:s"); 13:05:33


Here is complete date reference

d - The day of the month (from 01 to 31)
j - The day of the month without leading zeros (1 to 31)
D - A textual representation of a day (three letters)
l - (lowercase 'L') - A full textual representation of a day
S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)

m - A numeric representation of a month (from 01 to 12)
n - A numeric representation of a month, without leading zeros (1 to 12)
M - A short textual representation of a month (three letters Jan, Feb) 
F - A full textual representation of a month (January through December)
t - The number of days in the given month

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
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)
u - Microseconds (added in PHP 5.2.2)


z - The day of the year (from 0 through 365)
W - The ISO-8601 week number of year (weeks starting on Monday)

w - A numeric representation of the day (0 for Sunday, 6 for Saturday)
N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)

L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)
o - The ISO-8601 year number
U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

e - The timezone identifier (Examples: UTC, GMT, 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)
P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
T - Timezone abbreviations (Examples: EST, MDT)
Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
B - Swatch Internet time (000 to 999)


More PHP date working examples for your better understanding

Get any date few days, months ahead or backwards in PHP
$CDATE = date("Y-m-d");
$NDate = date("d", strtotime("+30 days"));
$NMonth = date("m", strtotime("+1 month"));
$NYear = date("Y", strtotime("+1 year"));

Get days of a Month or last date of month

$a_date = date("Y-m-t");

Get Last date of next month

$NMONTH = date('m', strtotime('+1 month'));
echo date("Y-".$NMONTH.'-t');

Get days, months or hours difference between two dates

$date1 = new DateTime('2012-06-01 02:12:51');
$date2 = $date1->diff(new DateTime('2014-05-12 11:10:00'));
echo $date2->d.'Total days'."\n";
echo $date2->y.' years'."\n";
echo $date2->m.' months'."\n";
echo $date2->d.' days'."\n";
echo $date2->h.' hours'."\n";
echo $date2->i.' minutes'."\n";
echo $date2->s.' seconds'."\n";
Calculate days, Months if number of days are known
$YEARS = floor($DAYS / 365);
$MONTHS = floor(($DAYS % 365) / 30.4166);
$DAYSP = floor(($DAYS % 365) % 30.4166);
Change Date Format
$date=date_create($PDATE);
$PDATE = date_format($date,"d-m-Y");
Change date format for microsoft access exported data to insert/import in MySQL
$X = '8/29/2011 11:16:12 AM';
$dt = new DateTime($X);
$date = $dt->format('Y-m-d');
$time = $dt->format('H:i:s');
$PDATE = $date.' '.$time;

strtotime is also an built in function, we will discuss it as well.