Conversion between PHP time and string

Source: Internet
Author: User
Tags add time

1, the difference of two dates, for example, 2007-3-5 ~ 2007-3-6 Date Differential
Echo ABS (Strtotime ("2007-3-5")-Strtotime ("2007-3-6"))/60/60/24;
echo "Day <br>";

2. JS gets the current date

var mydate = new Date ();
Mydate.getyear ();       //Get Current year (2-bit)
Mydate.getfullyear ();    //Get the full year (4-bit, 1970-????)
Mydate.getmonth ();      //Get the current month (0-11, 0 for January)
Mydate.getdate ();        //Get current day (1-31)
Mydate.getday ();         //Get Current week x (0-6, 0 for Sunday)
Mydate.gettime ();       //Get Current time ( Number of milliseconds since 1970.1.1)
Mydate.gethours ();      //Get current hours (0-23)
Mydate.getminutes ();    //Gets the current number of minutes (0-59)
Mydate.getseconds ();    //Gets the current number of seconds (0-59)
Mydate.getmilliseconds ();   //Gets the current number of milliseconds (0-999)
mydate.tolocaledatestring ();     //Get current date
var mytime=mydate.tolocaletimestring ();    //Get current time
Mydate.tolocalestring ();       //Get date and time

3, PHP date and time stamp mutual conversion

PHP time is large to have two, one is the timestamp type (1228348800), the second 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

It's time for the output to be in Beijing.

Conversion between PHP time and string

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.