Php date processing function (calculation time difference, conversion timestamp date)

Source: Internet
Author: User


The php tutorial converts the timestamp to a common date format and the calculation time difference: the default return type is "Minute"
Function trans_time ($ timestamp ){
If ($ timestamp <1) echo 'invalid unix timestamp ';
Else return date ("y-m-d h: I: s", $ timestamp );
}

// Obtain the ip address
Function get_ip (){
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;
}

// Calculation time difference: the default return type is minute"
// $ Old_time can only be a timestamp. $ return_type indicates that h is an hour, and s is a second.
Function timelag ($ old_time, $ return_type = 'M '){
If ($ old_time <1 ){
Echo 'invalid unix timestamp ';
} Else {
Switch ($ return_type ){
Case 'H ':
$ Type = 3600; break;
Case 'M ':
$ Type = 60; break;
Case's ':
$ Type = 1; break;
Case '':
$ Type = 60; break;
}
$ Dif = round (time ()-$ old_time)/$ type );
Return $ dif;
}
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.