PHP timestamp and normal time display conversion Code _php tutorial

Source: Internet
Author: User
Tags add time echo date
Strong AboutFire theNET (Liehuo Geneva. Net) teaches atProcess in the Web development, we submit the form is often converted to 10-digit timestamp format, when the call was stumped some classmates, then how to call the conversion out?

A: For example, if the variable you are calling is $liehuo_times, then the statement output in PHP is: , what's it, simple?

The time that is stored in MySQL is a timestamp, a 10-digit
How can I convert a function into a normal time function output?

Time ();
is to get the current time, but to get an integer type

This can be formatted
Date ("Y-m-d h:i:s", Time ());
So even when the seconds show together
Date ("Y-m-d", Time ()); Year-only date

echo "This machine is in the time zone:". Date ("T"). "
";
echo "php timestamp (Unix seconds):". Mktime (). "
";
echo "PHP time (after local format):". 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 types of storage in the database (really not only, my application is two kinds), 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 pay attention to, I usually use two, so, a few days ago built the table, the time type saved as date, I also always use time stamp to save, has been the data to write not into the table, debugging a long period of time only know the error, is the type does not meet, not to write storage.

Like the timestamp above is more convenient, but, when displayed, do not put this 1228348800 string to the customer to see, GOD nows!

So I used these two conversions, first of all, how to get the current timestamp, $date 1=time (), so that the current timestamp, to go back to the 2008-12-4 this format, using 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 '), the meaning of the inside parameters, if you do not understand the PHP manual.

Okay, let's go, turn 1228348800 into the 2008-12-4 format code as follows:

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

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

Time stamp to the normal date has, conversely, the normal date format to the timestamp, see the following code:

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

$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 in seconds,

Note: The time stamp above php5.1 will be 8 hours from the actual time, the solution is as follows
1, the simplest way is not to use php5.1 above version-obviously this is undesirable Method!!!

2, modify the php.ini. Open php.ini Find Date.timezone Remove the preceding semicolon = add Asia/shanghai After, restart Apache server can--the disadvantage is that if the program
Put on someone else's server, can not modify the php.ini, that will not be.

3, in the program to add time to initialize the statement that is: "Date_default_timezone_set (" Asia/shanghai "); "This can be arbitrarily set by the programmer, my push
Kanzian
Time zone identifiers, the values available in mainland China are: prc,asia/chongqing, Asia/shanghai, Asia/urumqi (Chinese, Chongqing, Shanghai, Urumqi, respectively), Etc/gmt-8,asia/harbin
Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macau, Hong Kong, Taipei, respectively)
and Singapore: Asia/singapore

http://www.bkjia.com/PHPjc/364669.html www.bkjia.com true http://www.bkjia.com/PHPjc/364669.html techarticle 74 Fire 96 net (Liehuo. net) teaches 23 way in the Web development, we submit the form the time often translates to the 10-digit timestamp format, when the call is baffled some classmate, then as ...

  • 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.