HOW TO: Convert Linux Date to text using PHP functions?

Author: Zeeshan Rasool  |  Last updated on PHP, Web Development

In PHP, purpose of date function is to format a UNIX time integer as text according to a specially encoded format string. Here you can see the process for this. We can use different parameters to get our required format.

Syntax:

Arguments:

$Format
A format string; the following key characters are substituted as described:

Key Description PHP
Date/Time r RFC 2822: D, d M Y H:i:s O
Date Year Y Year as 4 digit number
y Year mod 100 as 2 digit number
L 1 if a leap year, 0 if not
Month F Month of the year as full name
M Month of the year as three letter abbreviation
m Month of the year as one or two digit number
n Month of the year as two digit number
Day
of
Year
z Day of the year – 1 as number
Day
of
Month
d Day of the month as two digit number
j Day of the month as one or two digit number
jS Day of the month as one or two digit ordinal number
Day
of
Week
D Day of the week as three letter abbreviation
l Day of the week as full name
w Day of the week – 1 as number
Time Hour g Hour of the half day as one or two digit number
h Hour of the half day as two digit number
G Hour of the day as one or two digit number
H Hour of the day as two digit number
Minute i Minute of the hour as two digit number
Second s Second of the minute as two digit number
Millisecond u Millisecond of the second
AM/PM a am or pm
A AM or PM
Beats B Swatch time
Information t Number of days in the month
TimeZone T Timezone name
e Timezone abbreviatiom 5.1.0
O Offset from GMT in hundred hours
P Offset from GMT in hours:minutes 5.1.3
Z Offset from GMT in seconds
I 1 if Daylight Savings Time, 0 if not
UNIX time U Seconds since midnight before Jan 1, 1970 5.2.2
ISO
8601
Date/Time c ISO-8601 Date and time 5
Year o ISO-8601 year as 4 digit number 5.1.0
Day N ISO-8601: (((Day of the week) – 2) modulo 7) + 1 5.1.0

The return value is a string equal to the format string with substitutions from $Time (or current system time) according to the table above.

Example code:

Output:
When this page was loaded,
It was then Sat, 26 Dec 2009 07:09:51 +0000
The currend date was December 26, 2009
The currend date was Dec 26, 2009
The currend date was 12/26/09
The currend date was the 26th of Dec, 2009
The currend time was 7:09:51 AM GMT
The currend time was 07:09:51 +0000
2009 is not a leap year

Recommended WordPress Themes Store



If you enjoyed this post, please consider leaving a comment below or subscribing to the RSS feed to have future articles delivered to your feed reader. You can also follow us on Facebook or Twitter @99Points

This awesome post has been created by 99Points editor's team. You can follow our updates on Twitter and facebook, OR drop an email at 99points.info@gmail.com


Email Subscriber!

Be the first to know about new updates


Advertisement

Friends

Categories


Popular post