PHP Functions

Function is a programming tool which allows the user to perform a certain task. Contents of a function is actually a series of programming commands , whether long or short , which is arranged in such a way that it becomes a commandment . There are so many functions provided by PHP . Additionally PHP also provides features to create their own functions (often called UDF or User Defined Function) . The functions that are specific to a particular case may not be discussed or may also be discussed in another article that contains this specific case .
We will start our discussion of the date and time functions . The function of the date and time - according to its name - used for processing the date and time. Some functions will be discussed is a function checkdate ( ) , function date ( ) , and the function getdate ( ) .
  
Function checkdate ( )
Function checkdate ( ) is used to check the validity of a Gregorian date forms , or forms of international dates which we have adopted today . The syntax is as follows :
checkdate ( month, day , year)
Moon is the integer 1 to 12 representing the months January through December .
Today is an integer that indicates the number of days in a month . A valid number is 1 to 30 or 31 ( for February 1 to 28 or 29 ) .
Year is an integer number indicating the year . A valid number is 1 to 32 767 . Examples of use in the PHP script is as follows :

<HTML>
<HEAD>
<TITLE> CheckDate Function < / TITLE >
< / HEAD >
<BODY>
Will dated February 27, 1982 valid? <br />
<? php
$ tes_tanggal = checkdate ( 2,27,1982 ) ;
if ( $ tes_tanggal == true) {
echo " Yes , that date is valid ";
} else {
echo " No, that date is invalid ";
}
?>
<br />
<br />
Will dated June 13, 1982 valid? <br />
<? php
$ tes_tanggal = checkdate ( 6,13,1982 ) ;
if ( $ tes_tanggal == true) {
echo " Yes , that date is valid ";
} else {
echo " No, that date is invalid ";
}
?>
< / BODY >
< / HTML >
Function date ( )
date function is used to display the date and / or current time. The syntax usage is as follows :
date ( format [, timestamp ] )
Format is the characters that are used to format the display date and / or time .
Timestamp is the time measured by the number of seconds since the UNIX Epoch time , namely January 1, 1970 , 00:00:00 GMT . The point is that if the number 10 written timestamp it means the date in question is a January 1970 , 00:00:10 GMT . This time will adjust to local time , so if the local time is GMT +7 Indonesia , then if the number 10 written timestamp it means the date in question is a January 1970 , 07:00:10 . If the timestamp is not mentioned , it is taken from a local time at the moment.
Characters used for formatting is:
Character
Meaning
a
" am "or " pm "
A
"AM "or "PM "
B
Swatch Internet time
d
Days in a month , 2 digits with zero starts , from " 2001 "to" 31 "
D
Day of the week, textual , 3 letters ; eg " Sun " , "Sun "
F
Month , textual , full , eg " March " , " May "
g
Hour , 12 -hour format without the zero at the beginning , from " 1 "to" 12 "
G
Hour , 24 -hour format without the zero at the beginning , from " 1 "to" 23 "
h
Hour , 12 -hour format , from " 2001 "to" 12 "
H
Hour , 24 -hour format , from " 2000 "to" 23 "
i
Minutes, from " 2000 "to" 59 "
I
" 1 "if Daylight Savings Time, " 0 "if not.
j
Days in a month , without a preceding zero, from " 1 " to " 31 "
l
Day of the week, textual , complete , for example , "Friday " , " Monday " .
L
Value " 1 " for the leap year , " 0 " for no .
m
Months in the number " 01 "to" 12 "
M
Month , textual , 3 letters , eg " Jan ", " March "
n
Month in numbers without a preceding zero, from " 1 "to" 12 "
r
RFC 822 date format , eg " Thu , 21 Dec 2000 16:01:07 +0200 "
s
Seconds, from " 2000 "to" 59 "
S
Suffix which shows the numbers in English, textual , two letters, such as " th " , " nd "
t
Number of days in a month , from " 28 "to" 31 "
T
Time zone setting on your computer, ie " MDT "
U
The number of seconds since the Unix Epoch .
w
Today in the figures for one week, " 0 " for Sunday until " 6 " Saturday .
Y
Year , four digits , eg " 2001 "
y
Year , 2 digits , eg " 1999 "
z
Today in the figures for one year; from " 0 "to" 365 "
Z
Time zone setting in seconds, from " -43 200 "to" 43 200 " . West of UTC is negative and east of UTC is positive.
Sample usage is as follows :
<HTML>
<HEAD>
<TITLE> Date < / TITLE >
< / HEAD >
<BODY>
<font size="10px">
<? php
echo " Now is the date ";
echo date ( ' d - M - Y ') ;
echo " <br /> and hours ";
echo date ( ' h : i : s A ') ;
?>
< / FONT >
< / BODY >
< / HTML >

Function getdate ( )
Function getdate ( ) is used to retrieve the value of local time is now or at any time timestamp and the time put it into an associative array . The syntax is as follows :
getdate ( [ timestamp ] )
Array elements that can be used are as follows :
  1. " minutes " = minutes
  2. " seconds " = Seconds
  3. " mday " = days in a month
  4. " hours " = hour , 24 -hour format .
  5. " wday " = day of the week , numeric , 0 for up to six weeks for saturday
  6. " mon " = month , numerical .
  7. "Year " = year , numerical .
  8. " yday " = days in a year, for example, " 299 "
  9. " weekday " = day of the week , textual management , such as "Friday "
  10. " month " = month , full textual , eg "January "
For example, they want to be made welcome page that will greet visitors with the greeting Good Morning / Afternoon / Evening / Night accordance with the current time . Script is as follows :
<HTML>
<HEAD>
<TITLE> Getdate < / TITLE >
< / HEAD >
<BODY>
<center>
<h1>
<? php
$ now = getdate () ;
$ month = $ cur [' month '] ;
$ day = $ cur [' mday '] ;
$ year = $ cur [' year '] ;
$ hour = $ today [' hours '] ;
if ( $ hour < = 11 ) {
echo " Good Morning " ;
} elseif ( $ hours > 11 and $ hour < = 15 ) {
echo "Good Afternoon ";
} elseif ( $ hours > 15 and $ hour < = 18 ) {
echo " Good Afternoon ";
} elseif ( $ hours > 18 ) {
echo "Good Night ";
}
?>
< / h1 >
<h2> Welcome < / h2 >
<h3> Now is the date <? php echo "$ day $ month $ year "; ? > < / h3 >
< / BODY >
< / HTML >
Good luck ..... : - D

0 komentar:

Post a Comment