Date Time in PHP

Get and format date and time in PHP

PHP provides getdate(), date(), time() functions and DateTime class that can be used to get and format a date and time. The getdate() function stores the current date and time in an associative array. These values can be accessed by using the predefined keys of the array as shown below. The keys allow you get the day, month, year, seconds, minutes, and hours from the array.

Key Description Example
mday day of month (1-31) 23
wday day of week (0-6) 1
weekday day of week Monday
mon month of year (1-12) 12
month month of year December
year year 2013
seconds seconds (0-59) 20
minutes minutes (0-59) 40
hours hours (0-23) 11



echo “Today is ”.$f;

The date() function provides you more control over the format of a date and time. This is the general form of the date() function.

date(string_format, timestamp)

The format string allows you to specify a format of a date and time value. For example, you might want to format the current date as 23-12-2013 10:25:50 AM. The timestamp is an integer value that represents a date and time value. The time() function returns a time stamp of the current date and time. The special characters that can be used in the format string are shown below.

Character Description Example
a am or pm 10:30:50 am
A AM or PM 10:30:50 PM
s seconds (0-59) 56
i minutes (0-59) 45
h leading zero hours (01-12) 03
H leading zero hours (00-23) 09
g no leading zero hours (1-12) 3
G no leading zero hours (0-23) 9
d leading zero day of month (01-31) 02
j no leading day of month (1-31) 2
D three-character day Thu
w day of week (0-6) 2
l full text day Thursday
m month of year (01-12) 08
M three-character month of year Jan
F Full text month of year January
y two-digit year 13
Y four-digit year 2013


echo date("d/m/y h:i:s a",time());

From the above section, you have learnt to get and format the current date and time. Sometimes, however, you might want to format date and time that is not the current date. For example, you have a string that represents a date and want to format to whatever you want. Fortunately, PHP provides the DateTime class that allows you to convert the date string in to a DateTime object. The DateTime class has a method called getTimeStamp() that can be used to get the time stamp from the DateTime object.


$d=new DateTime(‘10/20/2013’);
echo date("d/m/y h:i:s a",$d->getTimeStamp());



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.