Home  >  Article  >  Backend Development  >  Two types of time analysis in PHP_PHP tutorial

Two types of time analysis in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:28:471007browse

So there are two forms of saving to the database (there are really more than two, my application only has two). I save the timestamp type as a string, which is more convenient.

Normal date type is saved as DATE type.

You should pay attention to these two PHP times. I usually use two, so in the table I built a few days ago, I saved the time type For DATE, I have been saving it with timestamp, but the data cannot be written into the table. After debugging for a long time, I found out the error. The type does not match and it is not written to the library.

Like the time above Stamping is more convenient, but when displaying it, don’t show this string of 1228348800 to the customer, GOD NOWS!

So the conversion of these two is used. First, let’s talk about how to get the current timestamp. ,$date1=time(); In this way, the current timestamp is obtained. To convert it back to the format of 2008-12-4, the date() function is used. The date() function is commonly used in PHP, such as To get the current date, you can use $date2=date('Y-m-d');. Regarding the meaning of the parameters inside, if you don’t understand, just check the php manual.

Okay, back to the topic, convert 1228348800 to The format code of 2008-12-4 is as follows:

$date3=date('Y-m-d H:i:s',"1228348800");

This is OK. If you still want to get the hour, Minutes and seconds, just change 'Y-m-d', but please note that the PHP time seems to have an error of 8 hours. Add it and it will be OK.

The timestamp is converted to the normal date. , on the contrary, to convert the normal date format into a timestamp, please see the following code:

$year=((int)substr("2008-12-04",0,4));// Get the year

$month=((int)substr("2008-12-04",5,2));//Get the month

$day=((int)substr( "2008-12-04",8,2));//What number was obtained

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 are minutes and seconds here.

Note: The timestamp of PHP5.1 and above will differ from the actual time by 8 hours. The specific solution for PHP time is as follows

1. The simplest way is not to use php5.1 or above - obviously this is not an advisable method! ! !

2. Modify php.ini. Open php.ini and search for date.timezone. Remove the semicolon = in front and add Asia/Shanghai at the end. Just restart the apache server. The disadvantage is that if the program is placed on someone else's server, php.ini cannot be modified, so there is nothing you can do.

3. Add a time initialization statement in the program, namely: "date_default_timezone_set("Asia/Shanghai"); "This can be set arbitrarily by the programmer, which I recommend.

Time zone identifier, the PHP time values ​​available in mainland China are: PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order, China, Chongqing, Shanghai, Urumqi), Etc/GMT-8, Asia/Harbin
Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in order, Macau, Hong Kong, Taipei)
And Singapore: Asia/Singapore
Australia: Australia/Sydney


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446418.htmlTechArticleSo there are two forms of saving to the database (really more than two, my application only has two), timestamp type I saved it as a string, which is more convenient. The normal date type is saved as DA...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn