The date() and time() functions allow you to get the date and time from the web server. These functions can be used to get dates and time in different formats.



These functions depend on timezone settings of the web server.

Current Date and Time

Syntax:

date(string format, int timestamp)

The date() function returns a formatted string representing a date; a passing second-time parameter is optional, the current local timestamp will be used if no timestamp is given.

Also, other characters like "-", ":" can be used between the letters to add additional formatting.

Example:

<?php
//current Date, i.e. 2013-08-01  
   echo date("Y-m-d");  
   
//current Time in 12 hour format, i.e. 08:50:55pm       
   echo date("h:i:sa"); 
   
//current Time in 24 hour format, i.e. 18:00:23    
   echo date("H:i:s");
        
//current Month, i.e. 08         
   echo date("m");      
   
//current Month name, i.e. Aug             
   echo date("M");              
?>

The Timestamp

The date() function always use the current timestamp, whether you passed it one or not.

Timestamp: A timestamp is a 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.

We can get current timestamp by using time() function.

Example:

<?php
//current Timestamp, i.e. 1377540167  
    echo time();        
?>

Increase/Decrease date using timestamp

Example:

<?php
    // Increase one day from current date
    echo "Date is ".date("Y-m-d", time()+86400); 
    ."<br>" 
    // Decrease 15 days from current date
    echo "Date is ".date("Y-m-d",time()-(86400*15)); 
?>

PHP Date string Reference

The date() function offers several possible formatting options. You can also customize the output from the format string. The following is the complete list of formatting options are available:

a am or pm
A AM or PM
B Swatch Internet time
d day of the month, two digits with leading zeros; i.e., "01" to "31"
D day of the week, textual, three letters; i.e., "Fri"
F month, textual, long; i.e., "January"
g hour, 12-hour format without leading zeros; i.e. "1" to "12"
G hour, 24-hour format without leading zeros; i.e., "0" to "23"
h hour, 12-hour format; i.e. "01" to "12"
H hour, 24-hour format; i.e. "00" to "23"
i minutes; i.e. "00" to "59"
I (capital i) "1" if Daylight Savings Time, "0" otherwise
j day of the month without leading zeros; i.e., "1" to "31"
l (lowercase 'L') day of the week, textual, long; i.e., "Friday"
L boolean for whether it is a leap year; i.e., "0" or "1"
m month; i.e. "01" to "12"
M month, textual, 3 letters; i.e. "Jan"
n month without leading zeros; i.e. "1" to "12"
r RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)
s seconds; i.e. "00" to "59"
S English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t number of days in the given month; i.e., "28" to "31"
T Timezone setting of this machine; i.e. "MDT"
U seconds since the epoch
w day of the week, numeric, i.e., "0" (Sunday) to "6" (Saturday)
Y year, 4 digits; i.e. "1999"
y year, 2 digits; i.e. "99"
z day of the year; i.e., "0" to "365"
Z timezone offset in seconds (i.e. "-43200" to "43200").

Example:

<?php  
  print date("m/d/y G.i:s<br>", time()); 
  //Outputs i.e. 12/26/13 18.36:50

  print "Today is ";
  print date("j \of\ F Y, \a\\t g.i a", time());
  //Outputs i.e. Today is 26 of December 2013, at 6.36 pm
?>

PHP date_default_timezone_set() Function

This function is required to use in the program to deal with different time zone. Sets the default timezone used by all date/time functions in a script.

Example:

<?php
  date_default_timezone_set("Asia/Bangkok");
  //or
  date_default_timezone_set('India/Kolkata');

  echo date_default_timezone_get();
?>  


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram