PHP Date and Time Function Syntax and Format String Example Code

PHP Date and Time Function

The date() and time() function is used to set the format of the date and time. By the date() function, you can set both format date and as well as time

PHP date() Function

The PHP date() is used to set the format of the date as well as time also.

Syntax of date() function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
      <?php
		 date(format, timestamp);
     ?>
  </body>
</html>

Caution:Format is required to specify the format of timestamp. And timestemp is optional, timestamp is a date and time of a particular event.

PHP Date Format

To define the date, the format parameter is required. The format parameters have different syntax like… (“y/m/d”), (“d-m-y”)and many more.

Example 1 – date() Function Code Syntax

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		echo date("y/m/d") . "<br />";
		echo date("d/m/y") . "<br />";
		echo date("m/d/y") . "<br />";
		echo date("d m, Y") . "<br />";
		echo date("d M, Y") . "<br />";
		echo date("d F, Y");
	?>
  </body>
</html>

In the above example, we show how the different ways you can set the date format as per the requirements.

PHP Date and Time Function Example Code
Fig.1 – date Function with Format String Example Code.

List of Day Parameters

Format String Parameters Description
d “d” represents to the date.
j “j” represents to date but without leading zeros like (1, 2, 3…).
D “D” represents to the day in the starting 3 characters from the day of the week(Mon-Sun).
l “l” represents to the day of the week in the string form.
w “w” represents to the day of the week, were zero(0) for sunday and six(6) for the staurday.
z “z” represents to the day of the year which is starting from zero(0) to 365.

List of Month Parameters

Format String Parameters Description
m “m” represents to the month in numeric form with leading zero like(01, 02…).
n “n” represents to month but without leading zero like(1, 2, 3…).
M “M” represents to the month in the starting 3 characters from the month of the year(Jan-Dec).
F “F” represents to the month of the year in the string form.
t “z” returns the number of the days in a month(28, 30 or 31).

List of Year Parameters

Format String Parameters Description
L “L” represents to the current year is a leap year or not, if it returns 1 then the year is a leap year or if it returns 0 then the year is not a leap year.
y “y” represents the 2 digit year format.
Y “Y” represents the 4 digit year format.

PHP Time Format

To define the time, the date() is used with the format parameter.

Example 2 – date() Function to show the time Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		echo date("h:i:sa") . "<br />";
		echo date("h:i:sA") . "<br />";
		echo date("G:i:sA") . "<br />";
		echo date("g:i:sA") . "<br />";
		echo date("r") . "<br />";
	?>
  </body>
</html>

In the above example, we show how the different ways you can set the time format as per the requirements.

PHP Date and Time Function with Format String Example Code
Fig.2 – date Function to display Time with Format String Example Code.

List of Time Parameters

Format String Parameters Description
r “r” represents both date and time simultaneously.
h “h” represents the hours with leading zeros like(01, 02, 03….).
H,G “H or G” represents the hrs in the 00 to 23hours format.
g “g” represents to the hours without leading zero like(1, 2, 3…).
a “a” represents the lowercase Ante meridiem and Post meridiem.
A “A” represents the Uppercase Ante meridiem and Post meridiem.
i “i” represents the minutes with leading zero like(01, 09,…).
s “s” represents the second with leading zero like(01, 02…).
You can leave a response, or trackback from your own site.

2 Responses to “PHP Date and Time Function Syntax and Format String Example Code”

  1. Youre so cool! I dont suppose Ive read anything like this before. So good to search out anyone with some unique ideas on this subject. realy thank you for starting this up. this web site is one thing that is needed on the web, somebody with a bit originality. useful job for bringing something new to the internet!

  2. Kristi Ader says:

    Very interesting subject , thankyou for posting.

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape