It is convenient to make php programs later and you can use them directly. In fact, some good functions are available in some mature cms systems, such as dedecms, Empire cms, and phpcms, both gb2312 and utf8 are supported. The code is as follows:
Function GetIP () {// obtain the IP address
If ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])
$ Ip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
Else if ($ _ SERVER ["HTTP_CLIENT_IP"])
$ Ip = $ _ SERVER ["HTTP_CLIENT_IP"];
Else if ($ _ SERVER ["REMOTE_ADDR"])
$ Ip = $ _ SERVER ["REMOTE_ADDR"];
Else if (getenv ("HTTP_X_FORWARDED_FOR "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("HTTP_CLIENT_IP "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("REMOTE_ADDR "))
$ Ip = getenv ("REMOTE_ADDR ");
Else
$ Ip = "Unknown ";
Return $ ip;
}
?>
The code is as follows:
Function DateAdd ($ date, $ int, $ unit = "d") {// increase of time (you can also improve it to increase by hour, minute, and second, and then add time)
$ DateArr = explode ("-", $ date );
$ Value [$ unit] = $ int;
Return date ("Y-m-d", mktime (0, 0, 0, $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);
}
Function GetWeekDay ($ date) {// calculate the day of the week
$ DateArr = explode ("-", $ date );
Return date ("w", mktime (0, 0, 0, $ dateArr [1], $ dateArr [2], $ dateArr [0]);
}
?>
The code is as follows:
Function check_date ($ date) {// check whether the date is valid
$ DateArr = explode ("-", $ date );
If (is_numeric ($ dateArr [0]) & is_numeric ($ dateArr [1]) & is_numeric ($ dateArr [2]) {
Return checkdate ($ dateArr [1], $ dateArr [2], $ dateArr [0]);
}
Return false;
}
Function check_time ($ time) {// check whether the time is valid
$ TimeArr = explode (":", $ time );
If (is_numeric ($ timeArr [0]) & is_numeric ($ timeArr [1]) & is_numeric ($ timeArr [2]) {
If ($ timeArr [0]> = 0 & $ timeArr [0] <= 23) & ($ timeArr [1]> = 0 & $ timeArr [1] <= 59) & ($ timeArr [2]> = 0 & $ timeArr [2] <= 59 ))
Return true;
Else
Return false;
}
Return false;
}
Function DateDiff ($ date1, $ date2, $ unit = "") {// Time comparison function, returns the seconds, minutes, hours, or days of difference between two dates
Switch ($ unit ){
Case's ':
$ Pidend = 1;
Break;
Case 'I ':
$ Pidend = 60;
Break;
Case 'H ':
$ Pidend = 3600;
Break;
Case 'D ':
$ Pidend = 86400;
Break;
Default:
$ Pidend = 86400;
}
$ Time1 = strtotime ($ date1 );
$ Time2 = strtotime ($ date2 );
If ($ time1 & $ time2)
Return (float) ($ time1-$ time2)/$ pidend;
Return false;
}
?>
PHP redirection
The code is as follows:
Method 1: header ("Location: index. php ");
Method 2: echo" Window. location = "$ PHP_SELF "; ";
Method 3: echo" ";
?>
Php excel file generation
The code is as follows:
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: filename=test.xls ");
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
// You can change the corresponding file header to export file formats such as .doc. xls.
?>
Time comparison problems
For example, a forum marks a post posted on the day with a new image.
Method 1:
The code is as follows:
// $ Db-> rows [$ I] [date] indicates the datetime field value in the database.
$ Today = time ();
$ TheDay = date ("Y-m-d H: I: s", $ today-24*3600 );
$ NewTag = $ db-> rows [$ I] [date]> = $ theDay? "":"";
// Method 2:
$ NewTag = $ db-> rows [$ I] [date]> = date ("Y-m-d 00:00:00 ")? "":"";
?>
The code is as follows:
// Extract the variables submitted by the page and browser, which is equivalent to enabling PHP. INI to enable global variables.
@ Extract ($ _ SERVER, EXTR_SKIP );
@ Extract ($ _ SESSION, EXTR_SKIP );
@ Extract ($ _ POST, EXTR_SKIP );
@ Extract ($ _ FILES, EXTR_SKIP );
@ Extract ($ _ GET, EXTR_SKIP );
@ Extract ($ _ ENV, EXTR_SKIP );
?>
// Read the file function
The code is as follows:
Function readfromfile ($ file_name ){
If (file_exists ($ file_name )){
$ Filenum = fopen ($ file_name, "r ");
Flock ($ filenum, LOCK_EX );
$ File_data = fread ($ filenum, filesize ($ file_name ));
Rewind ($ filenum );
Fclose ($ filenum );
Return $ file_data;
}
}
?>
// File writing function
The code is as follows:
Function writetofile ($ file_name, $ data, $ method = "w "){
$ Filenum = fopen ($ file_name, $ method );
Flock ($ filenum, LOCK_EX );
$ File_data = fwrite ($ filenum, $ data );
Fclose ($ filenum );
Return $ file_data;
}
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service