1. Mathematical operation function:
Mathematical operation is one of the most frequently performed functions in a program. PHP provides many system functions to realize operation functions, which can be used directly without installation, compilation and configuration. The following table lists the commonly used mathematical functions:
function name
functional description
function name
functional description
abs()
absolute value
Is _ finite ()
Determine whether it is a finite value.
acos()
Anti-casein
is_infinite()
Judge whether it is infinite.
Akos ()
Inverse hyperbolic cosine
is_nan()
Judge whether it is of legal value.
Asin ()
anti-sine
lcg_value()
Combinatorial linear congruence generator
Asin
Inverse hyperbolic sine
Log 10 ()
Logarithm based on 10
atan2()
Arctangent of two parameters
Log ()
natural logarithm
Atan ()
arc tangent
Max ()
highest
Atanh
Inverse hyperbolic tangent
Minimum value ()
minimum value
base_convert()
Basic _ transformation
mt_getrandmax()
Displays the maximum possible value of a random number.
bindec()
Convert binary to decimal
Mt _ rand ()
Generate better random numbers
Cell ()
Round in one way
mt_sran()
Sow a better random number seed.
cos()
cosine
octdec()
Convert octal to decimal
Coase ()
Hyperbolic cosine
Pi ()
circumference ratio
Debin ()
Decimal to binary conversion
Power ()
Exponential expression
Decimal system ()
Decimal to hexadecimal conversion
rad2deg()
Convert radians to corresponding angle values.
Decoct ()
Decimal to octal conversion
Rand ()
Generate a random number
deg2rad()
Convert angles to radians
Circle ()
Rounding floating-point number
exp()
Calculate the exponent of e
Sin ()
sine
Floor ()
Round by round
Singh ()
Hyperbolic sine
fmod()
Returns the floating-point remainder of division.
sqrt()
square root
getrandmax()
Displays the maximum possible value of a random number.
srand()
Sow the seeds of the random number generator
hexdec()
Convert hexadecimal to decimal.
Tan ()
tangent
hypo
Calculate the hypotenuse length of a right triangle
tanh()
Hyperbolic tangent
Note: These mathematical functions can only handle values in the range of integer and floating-point numbers in the computer.
Example: Function to Generate a Random Password
Function to create password ($ length = 6)// Generate password function, with 6 digits by default.
{
$ dictionary = " abcdefghijklmnopqrstuvwxyz 0 123456789 "; //password dictionary
$ max char = strlen($ dictionary); //Get the dictionary length
$ password =
for($ I = 0; $ i<$ length; $i++) // Randomly generate each bit.
{
$password。 =$dictionary[rand(0,$ maxChar)]; //Generate random numbers and then take letters from the dictionary.
}
return $ password
}
2. Date and time operation:
PHP provides rich functions for date and time manipulation.
1)UNIX timestamp:
Timestamp is a time representation method originated from UNIX system, which refers to the number of seconds that have passed from197065438+1October 1 (00:00:00GMT), so it is also called UNIX timestamp.
The easiest way to use timestamps in PHP is to call the time () function, which can return the representation of timestamps according to the current time. Example:
Echo "The current timestamp is:". time();
2) Date function:
Some common date functions provided by PHP.
① Date () function:
The date () function is used to format the local date and time. The syntax format is:
String date (string $ format [,int $ timestamp])
Returns the string generated by the integer $timestamp in the format given by $format. The $timestamp parameter is optional. If no timestamp is given, the local current time, i.e. time (), is used.
The following table lists descriptions of string parameters in $format format:
value
explain
Return value
D
On what day in January, there are two numbers with leading zeros.
0 1 to 3 1
D
Day of the week, text, 3 letters.
Monday to Sunday
J
On what day in January, there is no leading zero?
1 to 3 1
L
The lowercase letter l indicates the full-text format of the day of the week.
From Sunday to Saturday
ordinary
What day of the week does the number represent?
1 (Monday) to 7 (Sunday)
S
English suffix after the number of days per month, 2 characters.
First, second, third or fourth. You can use it with J.
W
The number shows the day of the week.
0 (Sunday) to 6 (Saturday)
Z
What day of the year?
0 to 366
W
What week of the year is this? Every week begins on Monday.
Example: 42 (42nd week of the year)
F
Month, full text format, such as January.
From January to December
M
Numeric month with leading zeros.
0 1 to 12
M
A month represented by a three-letter abbreviation.
1 month to1February
ordinary
The month represented by numbers, without leading zeros.
1 to 12
t
The number of days in a given month.
28 to 3 1
L
Is this a leap year?
Leap year 1, otherwise 0.
o
Year number
For example: 20 16
Y
The year represented by four digits.
For example: 20 16
y
Two-digit year
For example: 16
a
Lowercase morning and afternoon values
Morning or afternoon
A
Capitalized morning and afternoon values
Morning or afternoon
g
Hours, 12 hour format, without leading zeros.
1 to 12
G
Hours, 24-hour format, without leading zeros
0 to 23
h
Hours, 12 hour format with leading zeros.
0 1 to 12
H
Hours, 24-hour format, with leading zeros
00 to 23
I
Minute with leading zero
00 to 59 years old
s
Seconds with leading zeros
00 to 59 years old
e
Time zone identification
For example: UTC, GMT, Atlantic/Azores.
I
Is it daylight saving time now?
1 is daylight saving time, otherwise it is 0.
O
Hours different from Greenwich Mean Time
Example: +0200
Z
Seconds of time difference offset
-43200 to 43200
c
Date in ISO860 1
20 1 1-05- 12t 15: 19:2 1+00:00
r
Date in RFC822 format
For example: thu, 21dec201106: 07: 08+0200.
U
/kloc-seconds since 0/month 11970 00: 00: 00.
Same as the time () function.
Example:
$ today = date(" m/d/y "); //The result is: 07/ 19/ 12.
$today=date("F j,Y,g:I a "); //The results are: July 19, 20 12, 9:07 am.
$ today = date ("y year N month J day "); //The result is: 20 12 July 19.
$ today = date(" Y-m-d H:I:s "); //The result is: 2012-07-19 09: 07: 32.
$ today = date(" D M j G:I:s T Y "); //The result is thujul19 9: 07: 32 CST 2012.
$ today = date(' I t I s t h e jS d a y '); //The result is: 07 365 438+0 07 32 365 438+0 09 Asia/Shanghai 1 9 1 9AM 12.
$ today = date(' \ I \ t \ I \ s \ t \ h \ e jS \ d \ a \ y '); //The result is: it is day 19.
$ today = date(' H:m:s \ m \ I \ s \ \ m \ o \ H '); //The result is: 09:07:32 m is the month.
②gmdate () function:
The gmdate () function has the same function as the date () function, except that the time returned by the gmdate () function is Greenwich Mean Time (GMT). The syntax format is:
String gmdate (string $ format [,int $ timestamp])
③getdate () function:
The getdate () function is used to get the specified date and time information. The syntax format is:
Array acquisition date ([int $timestamp])
If timestamp $timestamp is not specified, the current local time of the system is used. The getdate () function returns an array, where each element represents a specific part of the date and time. The following table lists the descriptions of the key names in the returned array:
key name
explain
Examples of return values
second
Digital representation of seconds
0 to 59 years old
minute
Digital representation of minutes
0 to 59 years old
hour
The numerical representation of hours
0 to 23
mday
What day of the month does the number represent?
1 to 3 1
wday
What day of the week means
0 (Sunday) to 6 (Saturday)
Monks
The number of months shows that
1 to 12
year
A complete year in four digits.
For example: 20 16
yday
The number of days in a year indicates.
0 to 365
workday
Full-text representation of a day of the week
From Sunday to Saturday
moon
The full text representation of the month.
From January to December
The number of seconds since the beginning of the UNIX era.
Similar to the return value of time (), it is a timestamp.
Example:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
$ today = getdate();
Print _ r($ today);
Printf ("Today is the week of %s %s \ n", $ today ["year"], $ today ["mon"], $ today ["MDAY"], $ today ["WDAY"]);
Printf ("%s minutes and %s seconds when the time is %s ",$ today ["hours "], $ today ["minutes "], $ today ["seconds "]);
& gt
The result after implementation is:
Aral
(
[sec] = > 15
[Minute] = > 37
[Hours] = > nine
[mday]= & gt; 19
[wday]= & gt; four
[mon]= > seven
[Year] = > 20 16
[yday]= & gt; 200
[working day] = > Thursday
[month] = >; July
[0]= > 134266 1835
)
Today is 20 16, 19, Thursday, July.
Now the time is 9: 37, 15 seconds.
④checkdate () function:
The checkdate () function is used to check whether the date is valid, and if it is valid, it returns true, otherwise it returns false. The syntax format is:
bool checkdate(int $month,int $day,int $year)
The checkdate () function is considered as an effective date when the following conditions are met:
The value of $ year ranges from 1 to 32767.
The value of month is from 1 to 12.
The value of $ day is within the range of days in a given $ month, including leap years.
Example:
echo checkdate(2,29,20 12)? "Effective": "Invalid";
2) Time function:
① Time () function:
The use of the time () function is very simple, and there is no UNIX timestamp that returns the current date and time after calling the parameter. The syntax format is:
Intermediate time (invalid)
Example:
$ next 50 day = time()+(50 * 24 * 60 * 60); //Calculate the date after 50 days.
②mktime () function:
The mktime () function has the same function as the time () function, and can return the time indicated by the timestamp. The difference is that you can specify a specific date and time in the mktime () function. The syntax format is:
Int mktime([int $ hours], [int $ minutes], [int $ seconds], [int $ months], [int $ days], [int $ years])
The above parameter list is omitted from right to left, and any omitted parameter will be set as the current value of local date and time. Returns false if the specified time cannot be represented. Mktime () function is very useful in date calculation and verification. Example:
$day 1=mktime(0,0,0, 12,25,20 12);
$day2=mktime(0,0,0, 10, 1,20 12);
$ days =($ day 1-$ day 2)/(24 * 60 * 60); //Calculate the difference days according to the timestamp.
③strtotime () function:
The function converts a readable English date/time string into a UNIX timestamp. Syntax format:
int strtotime(string $time[,int $now])
Where the $ TERM parameter represents the string to be converted, and $ NUMBER represents the $ timestamp used to calculate the return value. If the $now parameter is not provided, the current time of the system will be used. The strtotime () function returns a timestamp on success, otherwise it returns false.
④microtime () function:
After calling the microtime () function, the current UNIX timestamp and the number of microseconds will be returned. The syntax format is:
Mixed micro-time ([bool $get_as_float])
The optional parameter $get_as_float here is a Boolean value, and if it is true, it will return a floating point number. By default, if there are no parameters, the string will be returned in the format of "millisecond seconds", where millisecond is the microsecond part and second is the UNIX timestamp.