PHP timestamp and conversion code for normal time display

Source: Internet
Author: User
Tags add time current time ini

For example, the variable you are calling is $liehuo_times, so the statement that is output in PHP is: "How about, simple?"

The time in MySQL is time stamp, 10-digit

How can I convert a function into a normal time function output?

Time ();

is to get the current time, but get an integral type

You can format this

Date ("Y-m-d h:i:s", Time ());

In this way, the seconds are displayed together

Date ("Y-m-d", Time ()); Only year years and days

echo "The time zone in which this computer resides:". Date ("T"). "

";

echo "php timestamp (Unix seconds):". Mktime ().

";

echo "PHP time (after local formatting):". Date ("Y-m-d h:i:s"). "

";

?>

There are two types of HP time, one is the timestamp type (1228348800), and the other is the normal date format (2008-12-4)

So there are two forms of saving to the database (really not only, my application on two), timestamp type I was saved as a string, this is more convenient.

The normal date type is saved as a date type.

These two should be noted, I usually use two kinds of, so, a few days ago to build the table, the type of time to save as date, I have been used to save the time stamp, has been written in the data table, debugging for a long time to know the error, is the type does not conform, do not write into the warehouse.

Like the time stamp is more convenient, but, show the time, do not put this 1228348800 string to the customer to see it, GOD nows!

So we used these two transformations, first of all, how to get the current timestamp, $date 1=time (), so that the current time stamp, to revert to the 2008-12-4 format, use the date () This function, in PHP, the date () function is more commonly used, If you get the current date, you can use $date2=date (' y-m-d '), about the meaning of the parameters inside, if you do not know, check the PHP manual.

Okay, well, turning 1228348800 into 2008-12-4 format code is as follows:

$date 3=date (' y-m-d h:i:s ', "1228348800");

This is OK, such as also want the hour, minute seconds, as long as the ' y-m-d ' change on it, but to note that PHP time like there are 8 hours of error. Plus, OK.

Time stamp to normal date has, on the contrary, the normal date format into a timestamp, please see the following code:

$year = ((int) substr ("2008-12-04", 0,4))//year of acquisition

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

$day = ((int) substr ("2008-12-04", 8,2))/date

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

This will turn the normal date into a timestamp, and if it is sometimes the same as the seconds,

Note: The php5.1 above time stamp will be different from the actual time difference 8 hours, the solution is as follows

1, the simplest way is not to use the php5.1 above version-obviously this is not advisable method!!!

2, modify php.ini. Open php.ini Find Date.timezone Remove the preceding semicolon = Add Asia/shanghai back, restart the Apache server-The disadvantage is that if the program

Put on someone else's server, can not modify the php.ini, it is not.

3, in the program to add time to the initialization of the statement is: "Date_default_timezone_set (" Asia/shanghai "); "This can be arbitrarily set by the programmer, my push

Recommended

Time zone identifier, the available values in mainland China are: prc,asia/chongqing, Asia/shanghai, Asia/urumqi (China, Chongqing, Shanghai, Urumqi), Etc/gmt-8,asia/harbin

Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macau, Hong Kong, Taipei)

and Singapore: Asia/singapore

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.