Date Function in PHP With Example

Date function in php with example - The function in the main date of operation in PHP and frequently used is the date () function. This function will generate the current date and time of the server. Some parameter options from the date () function can be seen in the following table:


Parameter
Information
Value example
Day
d
Date, 2 digits with zero
01 through 31
D
Three-digit day names of the week
Mon to Sun
j
Date without zero
1 to 31
l (lowercase 'L')
Full day name of the week
Sunday to Saturday
N
The order of days of the week
1 (for Monday) to 7 (for Sunday)
S
English numeric suffix for date, 2 characters
st, nd, rd or th.
w
The order of days of the week
0 (for Sunday) up to 6 (for Saturday)
z
The order of days of the year
0 to 365
Week
W
Week order of the year
Example: 42 (week 42 in this year)
Month
F
Full month name
January to December
m
The order of the month in a year with zero
01 to 12
M
Three-digit month names in a year
Jan to Dec
n
Order of the month in a year, without zero
1 to 12
t
The number of days in each month
28 to 31
Year
Y
4 (four) digit years
Example: 1999 or 2006
y
2 (two) digits year
Example: 99 or 06
Time
a
Lowercase Ante meridiem and Post meridiem
Am or pm
A
Uppercase Ante meridiem and Post meridiem
AM or PM
g
Clock format 12 without zero
1 to 12
G
Clock format 24 without zero
0 to 23
h
Clock format 12 with zero
01 to 12
H
Clock format 24 with zero
00 to 23
I
Minutes with zero
00 to 59
s
Seconds with zero
00 to 59

To better understand about date operation functions in PHP, here's an example of how to implement them in a program.

Date Function in PHP With Example

Program 1
File Name: date01.php
Description: The program displays date / time with various parameters.
Type the following php code in notepad.
<?php
// It is now 13th April 2006 09:43:49

echo "<br>". date("d/m/Y H:i:s"); // 13/04/2006 09:43:03
echo "<br>". date("F j, Y, g:i a"); // April 13, 2006, 9:43 am
echo "<br>". date("d.m.y"); // 13.04.06
echo "<br>". date("Ymd"); // 20060413
// 13-04-06, 4330 4349 4 Thuam06 102
echo "<br>". date('j-m-y, it is w Day z ');
// it is the 13th day.
echo "<br>". date('\i\t \i\s \t\h\e jS \d\a\y.');
// Thu Apr 13 9:43:49 KRAST 2006
echo "<br>". date("D M j G:i:s T Y");
echo "<br>". date("H:i:s"); // 17:16:17
?>

Date Function in PHP With Example

Save the php code into the htdocs folder named date01.php. To run the program open the browser then type http://localhost/date01.php in the address bar, then enter. Then the result will look like the following.
Date Function in PHP With Example
Program view

Program 2
File Name: date02.php
Description: The program displays the name of the day in Indonesian.
Open notepad, then type the following php code.
<?php
// It's now 13th April 2006 09:43:49
$arrDay = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday Saturday");
$day = date ("w");  //0 - 6 of day
echo "Today is the day : <b>" . $arrDay[$day]."</b>";
?>

Date Function in PHP With Example

Save the php code with the name date02.php in the htdocs folder. To see the result, open the browser, type http://localhost/date02.php in the address bar then enter. Then the result will look like this.
Date Function in PHP With Example
Program view

That's my article about date function in php with example. Hopefully the article can improve your skills in learning the php programming language. Learn also articles about: String Functions in PHP With Example

Comments