PHP Date and Time

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

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.

//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.

//current Timestamp, i.e. 1377540167  
    echo time();

Increase/Decrease date using timestamp

    // Increase one day from current date
    echo "Date is ".date("Y-m-d", time()+86400); 
    // 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:

aam or pm
BSwatch Internet time
dday of the month, two digits with leading zeros; i.e., "01" to "31"
Dday of the week, textual, three letters; i.e., "Fri"
Fmonth, textual, long; i.e., "January"
ghour, 12-hour format without leading zeros; i.e. "1" to "12"
Ghour, 24-hour format without leading zeros; i.e., "0" to "23"
hhour, 12-hour format; i.e. "01" to "12"
Hhour, 24-hour format; i.e. "00" to "23"
iminutes; i.e. "00" to "59"
I(capital i) "1" if Daylight Savings Time, "0" otherwise
jday of the month without leading zeros; i.e., "1" to "31"
l(lowercase 'L') day of the week, textual, long; i.e., "Friday"
Lboolean for whether it is a leap year; i.e., "0" or "1"
mmonth; i.e. "01" to "12"
Mmonth, textual, 3 letters; i.e. "Jan"
nmonth without leading zeros; i.e. "1" to "12"
rRFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)
sseconds; i.e. "00" to "59"
SEnglish ordinal suffix, textual, 2 characters; i.e. "th", "nd"
tnumber of days in the given month; i.e., "28" to "31"
TTimezone setting of this machine; i.e. "MDT"
Useconds since the epoch
wday of the week, numeric, i.e., "0" (Sunday) to "6" (Saturday)
Yyear, 4 digits; i.e. "1999"
yyear, 2 digits; i.e. "99"
zday of the year; i.e., "0" to "365"
Ztimezone offset in seconds (i.e. "-43200" to "43200").
  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.


  echo date_default_timezone_get();

Scroll Back to Top