Two types of PHP time parsing _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Two types of PHP time parsing. Therefore, there are two formats for saving the data to the database (more than my applications). The timestamp type is saved as a string, which is convenient. the normal date type is saved as DATherefore, there are two formats for saving the data to the database (more than my applications). The timestamp type is saved as a string, which is convenient.

The normal DATE type is saved as the DATE type.

Note that I usually use two types of PHP Time. Therefore, the time type of the table created a few days ago is stored as DATE, and I keep saving it with a timestamp, the data is not written into the table all the time. After debugging for a long time, the error is that the data type does not match and is not written into the database.

The timestamp above is more convenient, but when it is displayed, we will not show this 1228348800 string to the customer, god nows!

The two transformations are used. First, let's talk about how to obtain the current timestamp, $ date1 = time (); so that the current timestamp is obtained, the date () function is used to convert back to the format. in PHP, the date () function is commonly used, such as obtaining the current date, you can use $ date2 = date ('Y-m-D');. check the php manual if you do not understand the parameter.

Well, let's get down to the truth and convert 1228348800 to the format code as follows:

$ Date3 = date ('Y-m-d H: I: S', "1228348800 ");

This is OK. if you still want to get the hour and minute, you only need to change 'Y-m-D'. but pay attention to it, PHP time seems to have an error of 8 hours. add OK.

The time stamp is converted to the normal date. otherwise, convert the normal date format to the time stamp. See the following code:

$ Year = (int) substr ("",); // Obtain the year

$ Month = (int) substr ("",); // Obtain the month

$ Day = (int) substr ("",); // Obtain the number of digits

Echo mktime (0, 0, 0, $ month, $ day, $ year );

In this way, the normal date can be converted into a timestamp. The same is true if there is a time, minute, and second,

Note: The time stamp above php5.1 is 8 hours different from the actual time. the specific solution of PHP time is as follows:

1. the simplest method is not to use php5.1 or a later version-obviously this is not an advisable method !!!

2. modify php. ini. Open php. search for date in ini. remove the semicolon = Asia/Shanghai from timezone and restart the apache server. The disadvantage is that if the program is placed on another server, php cannot be modified. ini.

3. add the initialization statement of time to the program: "date_default_timezone_set (" Asia/Shanghai ");" which can be set by the programmer at will. I recommend this statement.

The time zone identifier. the available PHP time values in mainland China are PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (China, Chongqing, Shanghai, and Urumqi in sequence ), GMT-8, Asia/Harbin Etc/
Available in Hong Kong and Taiwan regions: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei)
Singapore: Asia/Singapore
Australia: Australia/sysydney


Merge (actually, my application only has two types). The timestamp type is saved as a string, which is convenient. the normal date type is saved as DA...

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.