Skip to content

How To Get Current Date And Time Using PHP?

In this article you are going to learn how to get current date and time using php programming language. This involves very simple steps and the functions used for this purpose are very easy to implement.

Functions which are used to get current date and time in php are listed below :

  1. date() function
  2. getdate() function

Both of these functions can be used to output the current date and time, but they have different syntax and different execution process. W3training School is one of best php training institute in gurgaon for professional who want to learn core and advanced features of php programming, but you can learn the date and time stamp functionality through this post.

DATE() FUNCTION

Syntax : date($format, $timestamp);

Primarily, date() function is used to set the format of date and time, but at the same time, it can be used to get the information about current date and time.

There are two parameters used in this function which are described below.

(1) $format : This string parameter is used to identify the format of the date and time. For Example, Y.m.d, Y/m/d, Y-m-d, etc. Some of the format characters which are generally used are given below.

For Year
y- 
it represents a year in two digits(i.e, 17)Y- it represents a year in four digits(i.e, 2017)

For month
m-
 it represents a month in two digits(i.e, 01 to 12)
M- it represents a month in three letters of alphabet(i.e, Jan to Dec)

For Day
d-
 it represents the day of the month in two digits(i.e, 01 to 31)
D- it represents the day of the month in three letters of alphabet(i.e, Mon to Sun)

For Time
h- 
it represents 12 hour format of time in two digits(i.e, 01 to 12)
H- it represents 24 hour format of time in two digits(i.e, 00 to 23)
i- minutes in two digits(i.e, 00 to 59)
s- seconds in two digits(i.e, 00 to 59)
a- lowercase ante meridiem and post meridiem(i.e, am or pm)

(2) $timestamp : This is an additional integer parameter which if is not provided repudiates to the current local time.

Result : Successful execution of the date() function will return date string in a particular preassigned date format.

Errors That Occur Using Date() Function : There are two possible errors that you might get while using date() function in a php script.

  • E_NOTICE : This error will be generated if you are using the time zone which is invalid. To prevent this notice error you can set an appropriate time zone in your php script by using the function date_default_timezone_set().
  • E_WARNING/E_STRICT : This error will be generated if you are using TZ environment variable or the system settings.
php-date-1024x515

Example :

php-dateoutput-1024x524

Output :

GETDATE() FUNCTION

Syntax : getdate($timestamp);

Getdate() function is use to get the current date and time depending upon the $timestamp variable. If timestamp is given, the function will return the date and time information of the timestamp. But, if it is not given, the function will return the current date and time.

There is only one parameter used in this function namely $timestamp. It is basically an additional integer parameter which if is not provided repudiates to the current local time.

Result : The successful execution of the getdate() function will return an associative array whose resultant elements are given below with their possible values.

seconds- represents seconds (0 to 59)
minutes- represents minutes (0 to 59)
hours- represents hours (0 to 23)
mday- day of the month in numerals (1 to 31)
wday- day of the week in numerals (0 ‘sunday’ to 6 ‘monday’)
mon- month in numerals (0 to 12)
year- represents year in four digits (i.e, 2017)
yday- day of the year in numerals (0 to 365)
weekday- day of the week in letters/characters (Sunday to Saturday)
month- month of the year in letters/characters (January to December)
0- seconds considering Unix Epoch (depends on the system)

Errors That Occur Using Getdate() Function : There are no such significant errors, however you must use the function properly in the supported php versions Php4, Php5 and Php7.

php-getdate-1024x519

Example :

php-getdateoutput-1024x516

Output :

We have tried to provide you the best possible information about how to get current date and time using php programming language. Hope you liked this post. For further updates, stay tuned to our php blogs.

Facebook
Twitter
LinkedIn
Pinterest