w3resource

php.js: date() function

date() function

In php.js date() function returns formatted local time/date.

Version

1103.1210

Syntax

date(format, timestamp)

Parameters

Name Description Required /
Optional
Type
format Day:
d - Numeric day of the month with
leading zeros (01 to 31)
D - Short day abbreviation (three letters). Mon through Sun.
j - Day of the month without leading zeros ( 1 to 31)
l (lowercase 'L') - Full day name (Sunday through Saturday)
N - ISO-8601 numeric representation of a day of a week (1 (for Monday) through 7 (for Sunday).
S - English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th. Works well with j)
w - Numeric day of the week. (0 (for Sunday) through 6 (for Saturday)
z - Numeric day of the year (0 to 365)

Week:
W - ISO-8601 numeric representation of week number of year. Week starting from Monday

Month :
F - Full month name. (January through December)
m - Numeric representation of a month with leading zeros (01 to 12)
M - Short month abbreviation (three letters). Jan through Dec
n - Numeric representation of a month, without leading zeros (1 through 12)
t -Number of days of a specified month (28 through 31)

Year :
L - Whether it's a leap year (set 1 if leapyear otherwise 0)
o - ISO-8601 year number
Y - Numeric year value in 4 digits (1999)
y - Numeric year value in two digit (1999 as 99)

Time :

a - Lowercase am or pm.
A - Uppercase AM or PM.
B - Swatch Internet time (000 through 999)
g - 12-hour format of an hour without leading zeros (1 to 12)
G - 24-hour format of an hour without leading zeros (0 to 23)
h - 12-hour format of an hour with leading zeros (01 to 12)
H - 24-hour format of an hour with leading zeros(00 to 23)
i - Minutes with leading zeros (00 to 59)
s - Seconds, with leading zeros (00 to 59)
u - Microseconds(numeric value) Example : 574925

Timezone :
e - The timezone identifier (Examples: UTC, Atlantic/Azores)
I - Whether the date is in daylights savings time (set 1 for Daylight Savings Time, 0 otherwise)
O - Difference to Greenwich time (GMT) in hours (Example: +0300).
p - Difference to Greenwich time (GMT) with colon between hours and minutes (Example: +03:00).
T - Timezone abbreviation. (Examples: EST, MDT)
Z - Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. (-43200 through 50400).

Full Date/Time :
c - ISO 8601 date (2004-02-12T15:19:21+00:00)
r - RFC 2822 formatted date. (Thu, 22 Jan 2005 16:01:07 +0200)
U - Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
Optional String
timestamp An integer indicates the unixtimestamp. If a timestamp is not supplied, it gives the current local time. Optional integer

Example of php.js date() function 

In the following web document date() function returns various formatted local date/time.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>php.js : date() function example</title>
<script type="text/javascript" src="datetime.min.js"> </script>
</head>
<body>
<h1 style="color: green">php.js : date() function example</h1>
<h3>Various date format using date() and it's parameters</h3>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
document.write("<strong>Display current date dd/mm/yyyy format </strong>"+"<br />");
document.write(date("d/m/Y")+"<br />");
document.write("<strong>Display current date mm/dd/yyyy format</strong> "+"<br />");
document.write(date("m/d/Y")+"<br />");
document.write("<strong>Display current date mm-dd-yyyy format </strong>"+"<br />");
document.write(date("m-d-Y")+"<br />");
document.write("<strong>Display like Monday 6th March 1996 </strong>"+"<br />");
document.write(date("l jS F Y")+"<br />");
document.write("<strong>Display the above format with time </strong>"+"<br />");
document.write(date('l jS F Y h:i:s A')+"<br />");
document.write("<strong>Display date in d-m-y format with user supplied date time</strong>"+"<br />");
document.write(date('d-m-y',mktime(0, 0, 0, 10, 31, 2010)));
//]]>
</script>
</body>
</html>

Output of the function:

Display current date dd/mm/yyyy format
04/07/2011
Display current date mm/dd/yyyy format
07/04/2011
Display current date mm-dd-yyyy format
07-04-2011
Display like Monday 6th March 1996
Monday 4th July 2011
Display the above format with time
Monday 4th July 2011 02:53:04 PM
Display date in d-m-y format with user supplied date time
31-10-2010

View the example of php.js date() function in the browser

Download phpjs.zip

Previous: php.js : checkdate() function
Next: php.js : getdate() function



Share this Tutorial / Exercise on : Facebook and Twitter