pTutorial

New way to learn PHP

PHP 5 Date and time

Working with date and time function

  • Web applications often deal with date and time, like current date.
  • PHP deal with date arithmetic that make manipulating dates easily.
  • Like date of accessing websites.
  • Like print current date.

PHP provides date () function to retrieve all the information about the date and time with the help of parameters like "d" for the day.

Print current date using date ()


   <?php

  	echo date ("l")."
"; echo date ("l dS F Y h:i:s A")."
"; echo date ("l \\t\h\e jS")."
"; ?>

In the above example, we are printing the current date in different format like only day or day with month with the help of the parameter.

Syntax of date function

date (format, timestamp); 
Format:-

String as per your requirement.

Timestamp:-

Timestamp is optional.

Day

Format Description Example
d Day of the month with leading zeroes (01 - 31) 21
D Three character of the day Sat
j Day of the month without leading zeroes (1 - 31) 21
l The full name of the day of the week (Sunday - Saturday) Friday Saturday
N Day of the week in numeric (Monday(1) - Sunday(7)) 6
S Suffix for day of the month (st, nd, rd, th) st
w Day of the week, numerical (Sunday (0) - Saturday (6)) 6
z Day of the year(0 - 365) 293

Example of day

<?php

echo "Today is:";
echo date ("l");

?>

Output

Today is:Saturday

You can use all the above parameter according to your need. For example, if you want to print the starting three letters of the day use "D" as a argument.

Week

Format Description Example
W week number of year 42

Week example

<?php

echo " number of Week in year:" .date ("W")."
"; ?>

Argument "W" is used for printing the numbers of week in the current year as shown in the above example.

Month

Format Description Example
F Full name of the month (January - December) October
m Numeric representation of the month(01 - 12) 10
M Three letter of the month Oct
n Numeric representation of month without 0 10
t number of the day 31

Month example

   <?php
   
echo "Full name of the month : - ";

echo date ("F")."
"; echo "Month in form of integer : - "; echo date ("m")."
"; echo "Three letter of the month : - "; echo date ("M")."
"; echo "Month in numbers: - "; echo date ("n")."
"; echo "Integer form of the day : - "; echo date ("t")."
"; ?>

The above example shows all the argument of the month for example, uses "F" as an argument for the full name of the month.

Year

Format Description Example
L Whether it is 1 (leap year) or its 0 ( not leap year) 0
Y Four digit representation of the year(1999 or 2014) 2017
y Two digit representation of the year(14) 17
o ISO-8601 version of 'Y', affected by the week ('W') (2014) 2017

Year example

  
<?php

echo "leap year or not- ";

echo date ("L")."
"; echo "Four digit of year: - "; echo date ("Y")."
"; echo "Last two digit of the year: - "; echo date ("y")."
"; echo "ISO-8601 version of 'Y', affected by the week ('W') : - "; echo date ("o")."
"; ?>

In the above example, argument "L" checks whether current year is a leap year or not, if the current year is leap year return 1 otherwise return 0.

Time

Format Description Example
a Lowercase am or pm pm
A Uppercase AM or PM PM
B Swatch internet time (000 – 999) 921
g 12-hour format of the hour, without leading zeroes (1 – 12) 9
h 12-hour format of the hour, with leading zeroes (01 – 12) 09
G 24-hour format of the hour, without leading zeroes (0 – 23) 21
H 24-hour format of the hour, with leading zeroes (00 – 23) 21
i Minutes with leading zeroes(00 – 59) 06
s Seconds with leading zeroes(00 – 59) 37
u Milliseconds (e.g. 54321) 000000

Time example

<?php
   
echo "The time is " . date ("h:i:sa");

?>

The above example shows the current time in 12-hour format, you can also print the current day in 24-hour format with the help of "G" and "H" argument.


QUOTES OF THE DAY

Knowledge without integrity is dangerous and dreadful.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER