MySQL and PHP pages at the same time correct Chinese &php use of the date function

Source: Internet
Author: User
Tags echo date

Document the problems and workarounds that are encountered in the project.

1. Display Chinese

In the PHP page display, we usually need to add the following code at the beginning to allow our page to display the correct Chinese:

<pre name= "code" class= "html" ><meta content = text/html; Charset=utf-8 http-equiv = content-type>


Yes, let the page show UTF-8, so that when we write PHP code such as this

echo ' hahaha ';
will be able to correctly display the Chinese on the page.

But there is a problem, when we store it in the database, if we write it directly in Chinese, it will look like this in the database:


This way, although the content you print from the PHP page is for you, if you look directly at the database, you do not know what the content is.

Similarly, if you enter in the database is able to read the GBK encoded in Chinese, if you print on the page to become this


What's going to change? We want to find a way to satisfy both:

1. Through the PHP code echo directly can print out Chinese

2. In the database display is able to understand the Chinese

3. Read the database to the page display is also able to understand the Chinese


There is no doubt that if we want to meet 1th, then we will undoubtedly join in the beginning:

<meta content = text/html; Charset=utf-8 http-equiv = content-type>

How to store UTF-8 encoded Chinese in the database we can understand it? There is a conversion between UTF-8 and GBK that needs to be converted two times.

When we save from the content of the page (UTF-8 encoding ) to the database (GBK) and read from the database (GBK) to the page (UTF-8):

We just need to use this function:

Iconv ()


To learn more about this function, please click on the following:


Php:iconv-manual


So, when we enter into the database, we make the following conversions:


Similarly, if you read from the database to the page, we do GBK to utf-8 conversion.



2. Php:date () function


The date function is used to convert from timestamp to the date format we want and then print out, here is the main record of the meaning of each word represented:

The parameters are as follows: A-"AM" or "PM" A-"AM" or "PM" D-a few days, two digits, if less than two digits before 0; such as: "01" to "D"-Day of the week, three letters of English; such as: "Fri" F-month, English full name; such as: "January" h-12 hours of the hour system; such as: "01" to "a" H-24 hours of the hour system; such as: "00" to "g-12" hours of the hour system, less than two does not fill 0; such as: "1" to three "G-24 hours of the hour, less than two does not fill 0; such as: "0" to "the" I-minute; such as: "00" to "the" J-a few days, two digits, if less than two bits do not fill 0; such as: "1" to "~" L-day of the week, English full name; such as: "Friday" M-month, two digits, if less than two digits in front of 0; such as: "01" to "three" N-month, two digits, if less than two will not fill 0; such as: "1" to "ten" M-month, three English letters; such as: "Jan" s-second; such as: "00" to "the" S-the end of the English ordinal, two English letters; such as: "th", "nd" T-Specify the number of days of the month; e.g. "28" to "U"-Total number of seconds W-number of days of the week, such as: "0" (Sunday) to "6" (Saturday) Y-year, four digits; such as: "1999" Y-year, two digits; such as: "" "Z-the first day of the year; For example: "0" to "365" if the displayed time is inconsistent with the system, it is necessary to change the php.ini configuration file. System default is UTC time can open php.inc setting   Date.timezone = PRC

So, if we want to output a time like May 27, 2015 15:45, we need to write this:

echo Date (' Y year M-month J-day, H:i ', $suggestions [' suggestion_time ']);

If you want to output other formats, please refer to the above parameters.


MySQL and PHP pages at the same time correct Chinese &php use of the date function

Related Article

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.