PHP Security Method (RFC 1123 format) for output English time and date _php instance

Source: Internet
Author: User
Tags rfc time and date

The most recent project is to write a client for a rest service, where the signature checksum requires a date in RFC 1123 format to be generated in the HTTP header.

This problem is better solved, simply browsing through the PHP document, using Gmstrftime This function can be solved.

Copy Code code as follows:

String Gmstrftime (string $format [, int $timestamp = time ()])

The code to invoke is as follows:
Copy Code code as follows:

Echo Gmstrftime ("%a,%d%b%Y%T%Z", Time ());
Output: Tue, APR 2014 16:16:07 GMT

During the debugging process, it was found that on another computer the output was not as expected, and then the Chinese were produced:
Copy Code code as follows:

Two, 4 2014 16:20:02 GMT

Continue browsing through the document, discovering the notes in the document, which are affected by the setlocale result, which is affected by the default language of the current system. Use the following command to view the languages that are installed on the current system:
Copy Code code as follows:

Locale-a

Then, based on the results of the analysis just analyzed, it is no problem to force the setlocale to be specified as English, the code is as follows:
Copy Code code as follows:

SetLocale (lc_time, ' en_US ');
Echo Gmstrftime ("%a,%d%b%Y%T%Z", Time ());

This article should have ended, but unfortunately the test on the Ubuntu machine, because it is the live CD version, there is no en_us the language, but there is a en_us. UTF-8. This time psychological drum, this practice seems not very safe, I have no way to determine the running code of the client, in the end there are en_US or en_US. UTF-8. It's good to see a piece of comments in a document that can be replaced with gmdate, which is not affected by setlocale results:
Copy Code code as follows:

Gmdate (' d, D M Y h:i:s '). ' GMT ';

Case closed~ Demand is small, it is not easy to write well, after more efforts.

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.