The MySQL and PHP pages correctly display the usage of the Chinese & amp; PHP date function at the same time,

Source: Internet
Author: User
Tags echo date

The MySQL and PHP pages correctly display the usage of the Chinese & PHP date function at the same time,

Record the Problems and Solutions encountered by the project.

1. display Chinese Characters

When displaying a PHP page, we usually need to add the following code at the beginning to make the page correctly display Chinese characters:

<pre name="code" class="html"><meta content = text/html; charset=UTF-8 http-equiv = Content-Type>

Yes, let the page display the UTF-8 so that when we write php code like this 

Echo 'haha ';
You can correctly display Chinese characters on the page.

However, there is a problem. When we store the data in the database, if we directly write the data into Chinese, it will look like this in the database:


In this way, even if the content you print out from the PHP page is for good, if you directly view the database, you will not understand what the content is.

Similarly, if you enter GBK-Encoded chinese characters that can be understood in the database, if you print them on a page, it becomes like this.


So what should we do? We want to find a method that meets the following requirements:

1. the php code echo can be used to print Chinese characters.

2. display Chinese characters that can be understood in the database

3. Read the database and display it on the page.


There is no doubt that to meet the 1st requirements, we have to join the following items at the beginning:

<meta content = text/html; charset=UTF-8 http-equiv = Content-Type>

So how to store the UTF-8 encoding of Chinese to the database after we can understand it? Here we need to perform two conversions between the UTF-8 and GBK.

In ourPageContent (UTF-8 Coding) SavedDatabase (GBK)Time and read from the database (GBK)Pages (UTF-8):

We only need to use this function:

Iconv ()


For more information about this function, click:


PHP: iconv-Manual


Therefore, we perform the following conversions when entering the database:


Similarly, if we read data from the database to the page, we will convert gbk to UTF-8.



2. PHP: date () function


Date functionIt is used to convert the timestamp into the desired date format and print it out. Here we mainly record the meaning of each word:

The parameters are as follows: a-"am" or "pm" A-"AM" or "PM" d-a few days, two digits. If there are less than two digits, the first digit is set to zero. For example: "01" to "31" D-day of the week, three English letters; for example, "Fri" F-month, full English name; for example: "January" h-12 hours; for example, "01" to "12" H-24 hours; for example: "00" to "23" g-12 hours, less than two do not fill zero; for example: "1" to 12 "G-24 hours, for example, "0" to "23" I-minutes; for example, "00" to "59" j-days, two digits. If the remaining two digits are not zero; for example: "1" to "31" l-the day of the week, full name in English; for example: "Friday" m-month, two digits. If less than two digits are left blank in front, for example: "01" to "12" n-month, two digits. If not For example, "1" to "12" M-month, three English letters, for example, "Jan" s-seconds; for example: "00" to "59" S-ending with an English ordinal number, two English letters; for example: "th", "nd" t-specifies the number of days of the month; for example: "28" to "31" U-Total number of seconds w-number type day of the week, for example: "0" (Sunday) to "6" (Saturday) Y-year, four digits, for example, "1999" y-year, two digits. For example, "99" z-the day of the year. For example: "0" to "365" if the display time is different from the system, you need to change the PHP. ini configuration file. The default UTC time is used. You can enable php. inc to set date. timezone = PRC.

Therefore, if we want to output the time in the format of, January 1, May 27, 2015, we need to write:

Echo date ('y, m, j, H: I ', $ suggestions ['suggestion _ time']);

If you want to output other formats, just refer to the above parameters!


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.