date() function: How To Get Current Date in PHP

The date() is a function of PHP. It is used to get the current date. You have to use the date format in the date() function’s parameter as a string. This date() function/method gives you the versatility to print day, today’s date, the current date (in any format) and so more you will know further.

Remember that in date format “Y” (means year) should be a capital letter if you want to get a year in four digits (ex.-2020) or for, year in two digits (ex.- 20), Then small “y“.

Examples of date() function in PHP –

Two Digits Year in date

<?php
$date=date("d/m/y");
echo $date;
?>

four digits Year in date

<?php
$date=date("d m Y");
echo $date;
?>

You can use a hyphen (d-m-y), slash (d m y), slaces (d/m/y), dot (d.m.Y) and any other symbol whichever you want to be inserted to separate date, month & year.

Print only Today’s date

<?php
$date=date("d");
echo $date;
?>

Print only current month

<?php
$month=date("m");
echo $month;
?>

Print only this year

<?php
$year=date("Y");
echo $year;
?>

When you want to know the date of today without 0 (if a date is between 0 to 9). Then use the “j” character in the argument.
For example, 3 not 03 ⨠⨠

<?php
$date=date("j");
echo $date;
?>


Get day (Short)
EX. – Fri

<?php
$day=date("D");
echo $day;
?>

Get day (Full)
EX. – Friday

<?php
$day=date("l");
echo $day;
?>

Current Month Name (Short)

<?php
$short_name=date("M");
echo $short_name;
?>

Current Month Name (Full)

<?php
$mon_name=date("F");
echo $mon_name;
?>

But while storing data in the database, you need to use format Y-m-d and hyphen to separate each of them.

Furthermore, if you use a forward slash (/) or any other symbols for separating dates then, the database software like MySql converts these into a hyphen. And conclusively, the date format will look like Y-m-d.

Some other characters to be used as a formatExamples
N (capital)See example
S (capital)See example
W (capital)See example
z (small)See example
L (capital)See example
t (small)See example

N as format

This returns the day of the week as number. For example, If today is Friday. Then it will give output 5 because Friday comes on 5th number in the week. The counting starts from Monday (returns 1 for).

<?php
$date=date("N");
echo $date;
?>

S as format

for 1, the suffix we use is “st“, for 2, the suffix is “nd“, suffix for 3 is “rd“, etc. This is what the S character returns when it is used in the argument of the date(). Assume, today is the third day of the month then the suffix that it will return is “rd“.

<?php
$date=date("S");
echo $date;
?>

W as format

This return the week number from a year. 1 Year contains 52 weeks.

<?php
$week_no=date("W");
echo $week_no;
?>

z as format

This returns the day number of a year. There are 365/366 years in a year

<?php
$day_no=date("z");
echo $day_no;
?>

L as format

This is used to examine whether this year is leap year or not. It returns 0 (means not a leap year) or 1 (means it is leap year).

<?php
$check_year=date("L");

if($check_year==1)
{
	echo "This is a leap year.";
}
else{
	echo "This is not a leap year";
}

?>

t as format

Checks the total number of days in current month.

<?php
$days=date("t");
echo $days;
?>

Time Related character to be used in the date() argument

Current Time with date

Use H (capital) for 24 hours time format

<?php
date_default_timezone_set('Asia/Calcutta');
$time=date("d/m/Y h:i:s");
echo $time;

?>

Use “a” for am and pm
Or
Use “A” for AM and PM

<?php
date_default_timezone_set('Asia/Calcutta');
$time=date("h:i:s A");
echo $time;

?>

Use “T” to Print Time Zone

<?php
date_default_timezone_set('Asia/Calcutta');
$time_zone=date("h:i:s T");
echo $time_zone;

?>

Leave a Comment