The PHP Date() Function


 

The PHP Date() Function

The PHP date() function is used for formatting the date and time. In this page we will show you the PHP Date function examples.

The PHP date() function is used for formatting the date and time. In this page we will show you the PHP Date function examples.

The PHP Date() Function

The PHP date() function is used for formatting the date and time. It formats a timestamp (a sequence of characters representing the date and/or time at which a certain event occurred) to make more readable date and time. No installation is necessary for using this function. 

Syntax

date(format,timestamp)

Parameter

format - It is essential in declaring the date that specifies the format of the timestamp.

timestamp - It is optional and should be used for declaring specific date and time. By default timestamp is current date and time. 

Some characters are used in date functions:

d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
Y - Represents a year (in four digits)

Some special characters like"/", ".", or "-" are also been used for adding additional formatting. For example:

<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d")
?>

The output of the code will look like this:
2009/09/22
2009.09.22
2009-09-22

Runtime Configuration

The behavior of the date/time functions is affected by settings in php.ini. It is configured on the basis of five options:

date.default_latitude - It specifies the default latitude functions. This option is used by date_sunrise() and date_sunset(). By default it is "31.7667".

date.default_longitude - It specifies the default longitude functions. This option is used by date_sunrise() and date_sunset(). By default it is "35.2533".

date.sunrise_zenith - It specifies the default sunrise zenith. This option is used by date_sunrise() and date_sunset(). By default it is "90.83"

date.sunset_zenith - It specifies the default sunset zenith. This option is used by date_sunrise() and date_sunset(). By default it is "90.83"

date.timezone - It specifies the default timezone. It was first time introduced in PHP 5.1 version while all the above was introduced in  PHP 5.0. Each country has its own time zone depending upon the latitude, longitude, sunrise and sunset components. 

PHP Date() - Adding a Timestamp

The date () usually use the current date and time of the system while making it specific,  timestamp parameter is used in the date() function. e.g. 
The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

More Parameters

Here you are the detail description of parameters are given: 

Parameters for Day

d - the parameter d denotes the day of the month. it contains two digits from 01 to 31. The zero digit leads in 01 to 09 dates. 

D - it represents a textual representation of a day containing first three letters like Mon, Tue, Wed, Thu, Fri, Sat and Sun.

j - this parameter is used for reflecting day of the month without leading zeros like 1 to 31. 

l or L - used for displaying full textual representing of the day of the week. 

N - this is ISO-8601 numeric representation of the day of the week which was included in PHP 5.1.0  in which 1 denotes to Monday while 7 to Sunday. The remaining days are denoted through 2 to 6 respectively. 

S - S denotes for English ordinal suffix for the day of the month, 2 characters like st, nd, rd or th. It works well with j.

w - It denotes the numeric representation of the day of the week like 0 for Sunday to 6 for Saturday. 

z - It denotes the day of the year beginning from 0 through 365.

0


Parameters for Week

W - ISO - 8601 week number of year, weeks starting on Monday. e.g 52 - the 52nd week in the year.

Parameters for Month

1

F - The parameter F denotes a full textual representation of a month, such as January or March, e.g. January, February March .....December.

m - It represents a month in digits from 01 to 12 containing leading zero in 01 to 09.  

M - It displays a short textual representation of a month containing three letters e.g. Jan through Dec

2

n - the small 'n' parameter is used for numeric representation of a month without leading zeros

t - it is used for number of days in the given months like 28 for February, and 30 and 31 for other months. 29 for leap year February. 


Parameters for Year

3

L - This parameter is used for checking the leap year. 

Y - it represents the year in complete four digit like 1990, 2000, 2009 etc.

o - ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. e.g. 1990, 2000, 2009 etc.

4

y - It represents the year in only two digits - the last two digits of the year; e.g. 91, 09


Parameter for Time

a -  It represents Lowercase Ante meridiem and Post meridiem like am or pm.

5

A- It represents Uppercase Ante meridiem and Post meridiem like AM or PM

B - Swatch Internet time. e.g. 000 through 999

g - It is used for showing 12-hour format of an hour without leading zeros 1 through 12.

6

G - It is used for showing 24-hour format of an hour without leading zeros 1 through 12.

h - It is used for adapting 12-hour format of an hour with leading zeros. For example: 01 through 12

H - It is used for adapting 24-hour format of an hour with leading zeros e.g. 00 through 23

7

i - It is used for displaying Minutes with leading zeros like 00 to 59

s - It is used for displaying seconds with leading zeros like 00 to 59

u - It is used for displaying Microseconds like 546325

8


Parameter for Timezone

e - It is used for Timezone identifier like UTC, GMT, Atlantic/Azores

I - It is especially used for whether or not the date is in daylight saving time for example: 1 if Daylight Saving Time, 0 otherwise.

9

O - It is used for representing the difference to Greenwich time (GMT) in hours like Example: +0200

P - It is used for representing the difference to Greenwich time (GMT)  in this format: +02:00

T - It is used for displaying Timezone abbreviation like EST, MDT..

0

Z - It is used for Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. For example: - 43200 through 50400


Parameter for Full Date/ Time 

c - It is used for showing the Time in ISO 8601 date format like 2004-02-12T15:19:21+00:00

1

r - It is used for RFC 2822 formatted date like Thu, 21 Dec 2000 16:01:07 +0200

U - It is used for seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)


Note: Unrecognized characters in the format string will be printed as-is. The Z format will always return 0 when using gmdate().


Return Values
It returns a formatted date string. If a non-numeric value is used for timestamp, FALSE is returned and an E_WARNING level error is emitted.


Errors/Exceptions
The time() creates error if time zone is not valid and generates E_NOTICE  everytime. It generates a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable.

2

 

Ads