Apr
13

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

Author ZeeShaN    Category PHP, Web Development     Tags ,

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:

string date (string $Format);
string date (string $Format, int $Time);
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:

<?php

echo "When this page was loaded,n";
echo 'It was then ', date ('r'), "n";
echo 'The currend date was ', date ('F j, Y'), "n";
echo 'The currend date was ', date ('M j, Y'), "n";
echo 'The currend date was ', date ('m/d/y'), "n";
echo 'The currend date was the ', date ('jS of M, Y'), "n";
echo 'The currend time was ', date ('g:i:s A T'), "n";
echo 'The currend time was ', date ('H:i:s O'), "n";
echo date ('Y');
date ('L')?(print ' is'):(print ' is not');
echo " a leap yearn";
?>

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

Add To Facebook Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo Add To Twitter


Written by ZeeShaN

ZeeShaN RasooL is a web developer who loves to work in latest technologies to create more interactive dynamic and beautiful web pages.







Enter your Email:

Click Here for Popular

Who I Am

Zeeshan Rasool

Software Engineer - PHP
Lahore - Pakistan

zeeshan(@)99points.info
Skype: zeeshan-rasool
gTalk: zishan.rasool85

Categories

Tags

Comments