PHP Date Time Functions

PHP 5 Date/Time Functions

In PHP 5, the date/time functions allow us to get the date and time from the server where our PHP script runs. We can easily use the date/time functions to format the date and time in different ways. As being a part of the PHP core, no installation is required to use these functions.

PHP 5 Date/Time Functions

PHP date()

The date() function formats a local date and time, and returns the formatted date string.

date() has the following basic syntax:

date(format,timestamp);

where;

format: It specifies the format of the outputted date string.

Example

 

PHP checkdate()

The checkdate() function is used to validate a Gregorian date.

checkdate() has the following basic syntax:

checkdate(month,day,year);

where;

month: It specifies the month as a number between 1 and 12.

day: It specifies the day as a number between 1 and 31.

year: It specifies the year as a number between 1 and 32767.

Example

 

PHP date_create()

The date_create() function returns a new DateTime object.

date_create() has the following basic syntax:

date_create(time,timezone);

where;

time: It specifies a date/time string and NULL indicates the current time.

timezone: It specifies the timezone of time.

Example

 

PHP date_format()

The date_format() function returns a date formatted according to the specified format.

date_format() has the following basic syntax:

date_format(object,format);

where;

object: It specifies a DateTime object returned by date_create().

format: It specifies the format for the date.

Example

 

PHP getdate()

The getdate() function returns date/time information of a timestamp or the current local date/time.

getdate() has the following basic syntax:

getdate(timestamp);

where;

timestamp: It specifies an integer Unix timestamp and default is the current local time (time()).

Example