Mutual conversion between PHP time and string

Source: Internet
Author: User
Tags types of tables
1. Calculate the difference between two dates, for example, 2007-3-5 ~ Echoabs (strtotime (2007-3-5)

1. Calculate the difference between two dates, for example, 2007-3-5 ~ Echo abs (strtotime (2007-3-5)-strtotime (2007-3-6)/60/60/24; echo day br; 2. JS obtains the current Date var myDate = new Date (); myDate. getYear (); // obtain the current year (2 digits) myDate. getFullYear (); // get

1. Calculate the difference between two dates, for example, 2007-3-5 ~ Date difference of-3-6
Echo abs (strtotime ("2007-3-5")-strtotime ("2007-3-6")/60/60/24;
Echo "Day
";

2. JS obtains the current date

Var myDate = new Date ();
MyDate. getYear (); // Obtain the current year (2 digits)
MyDate. getFullYear (); // Obtain the complete year (4-digit, 1970 -????)
MyDate. getMonth (); // Obtain the current month (0-11, 0 represents January)
MyDate. getDate (); // Obtain the current day (1-31)
MyDate. getDay (); // Obtain X of the current week (0-6, 0 indicates Sunday)
MyDate. getTime (); // Obtain the current time (the number of milliseconds since January 1)
MyDate. getHours (); // Obtain the current hour (0-23)
MyDate. getMinutes (); // Obtain the current number of minutes (0-59)
MyDate. getSeconds (); // Obtain the current number of seconds (0-59)
MyDate. getMilliseconds (); // Obtain the current number of milliseconds (0-999)
MyDate. toLocaleDateString (); // Obtain the current date
Var mytime = myDate. toLocaleTimeString (); // Obtain the current time
MyDate. toLocaleString (); // Obtain the date and time

3. PHP date and time stamp Conversion

There are two types of PHP time difference: one is the timestamp type (1228348800) and the other is the normal date format)

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 the DATE type.

Note that I usually use two types of tables. Therefore, the table created a few days ago stores the time type as DATE, and I keep using the timestamp to save it, 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!

So the two transformations are used. First, let's talk about how to get the current timestamp, $ date1 = time (); so that we can get the current timestamp, 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 also has 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 timestamp above php5.1 is 8 hours different from the actual time. The solution 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. ini to search for date. timezone and remove the semicolon = followed by Asia/Shanghai. Restart the apache server. The disadvantage is that if the program
Put it on someone else's server and cannot modify php. 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.
Recommendation.
Time zone identifier, available value in mainland China: PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (China, Chongqing, Shanghai, Urumqi), Etc/GMT-8, asia/Harbin
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

In this way, the output is Beijing time.

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.