Date() Function in PHP

Introduction

 
In PHP, the date() function is used for formatting the date and time. The date() function also formats a timestamp.
 
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. A timestamp is the number of seconds from January 1, 1970 at 00:00. Otherwise known as the Unix Timestamp, this measurement is a widely used standard that PHP has chosen to utilize.
 
Syntax
 
date (format,timestamp)
 
Simple Example of Date() Function
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>Format of Date</h3>  
  4. <?php  
  5. echo "First Format  : "date ("d/m/Y") . "<br />";  
  6. echo "Second Format  :".date ("d.m.Y") . "<br />";  
  7. echo "Third Format  :".date ("d-m-Y");  
  8. ?>   
  9. </body>  
  10. </html>  
Output
 
date function in php 
 

mktime() function

 
The mktime function is used to create a timestamp for tomorrow/yesterday. The mktime() function returns the timestamp for a date.
 
Syntax
 
mktime (hour, minute, second, month, day, year, is_dst)
 
Example
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>mktime() function : </h3>  
  4. <?php  
  5. $today = mktime(0,0,0,date("m"), date("d"), date("Y"));  
  6. echo "Today date is :".date("d-m-Y"$today)."<br/>";  
  7. $yesterday = mktime(0,0,0,date("m"), date("d")-1, date("Y"));  
  8. echo "The date of yesterday was :".date("d-m-Y"$yesterday)."<br/>";  
  9. $tomorrow = mktime(0,0,0,date("m"), date("d")+1,date("Y"));  
  10. echo "Tomorrow date will be : ".date("d-m-Y"$tomorrow);  
  11. ?>  
  12. </body>  
  13. </html>  
Output
 
mktime function in php 
 

getdate() function

 
The getdate() function return the date and time.
 
Syntax
 
getdate (timestamp)
 
Example 1
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>getdate() function : </h3>  
  4. <?php  
  5. $today = getdate();  
  6. print_r ($today)."<br/>";  
  7. ?>  
  8. </body>  
  9. </html>  
Output
 
getdate function in php 
 
Example 2
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>getdate() function : </h3>  
  4. <?php  
  5. $my_t=getdate (date("U"));  
  6. print ("$my_t [weekday], $my_t [month] $my_t [mday], $my_t [year]");  
  7. ?>  
  8. </body>  
  9. </html>  
Output
 
getdate function in php 
 

Various date format in PHP

 
DAYS
 
d - day of the month 2 digits (01-31)
j - day of the month (1-31)
D - 3 letter day (Mon - Sun)
l - full name of day (Monday - Sunday)
N - 1=Monday, 2=Tuesday, etc (1-7)
S - suffix for date (st, nd, rd)
w - 0=Sunday, 1=Monday (0-6)
z - day of the year (1=365)
 
WEEK
 
W - week of the year (1-52)
 
MONTH
 
F - Full name of month (January - December)
m - 2 digit month number (01-12)
n - month number (1-12)
M - 3 letter month (Jan - Dec)
t - Days in the month (28-31)
 
YEAR
 
L - leap year (0 no, 1 yes)
o - ISO-8601 year number (Ex. 1979, 2006)
Y - four digit year (Ex. 1979, 2006)
y - two digit year (Ex. 79, 06)
 
TIME

a - am or pm
A - AM or PM
B - Swatch Internet time (000 - 999)
g - 12 hour (1-12)
G - 24 hour c (0-23)
h - 2 digit 12 hour (01-12)
H - 2 digit 24 hour (00-23)
i - 2 digit minutes (00-59)
s 0 2 digit seconds (00-59)
 
OTHER
 
e - timezone (Ex: GMT, CST)
I - daylight savings (1=yes, 0=no)
O - offset GMT (Ex: 0200)
Z - offset in seconds (-43200 - 43200)
r - full RFC 2822 formatted date
 

Conclusion

 
So in this article you saw, how to format a date in PHP. Using this article one can easily understand the date format in PHP.
 
Some Useful Resources


Similar Articles