Php timestamp conversion date method summary-PHP source code

Source: Internet
Author: User
In php, the date function can directly convert the timestamp into a date. Of course, there are some other methods. I won't talk much at the beginning of the article to avoid getting more and more difficult to understand, let's take a look at some examples. In php, the date function can directly convert the timestamp into a date. Of course, there are some other methods. I won't talk much at the beginning of the article to avoid getting more and more difficult to understand, let's take a look at some examples.

Script ec (2); script

Current Timestamp

The Code is as follows:

$ Nowcode = time ();
$ Nowcode = strtotime (now );


Time stamp converted to date

The Code is as follows:

$ Date = date ("Y-m-d", getdatecode ($ ));

Convert date to Timestamp

The Code is as follows:
Function getdatecode ($ time)
{
$ Year = (int) substr ($ time,); // obtain the year
$ Month = (int) substr ($ time, 5, 2); // obtain the month
$ Day = (int) substr ($ time, 8, 2); // obtain the number of digits
Return mktime (, 0, $ month, $ day, $ year); // returns the UNIX timestamp.
}

Well, let's get down to the truth and convert 1228348800 to the format Code as follows:

The Code is 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.

You can add

The Code is as follows:
Date_default_timezone_set ("Asia/Shanghai ");


Below are moreTimestamp conversion dateExample

The Code is as follows:

(1) print the timestamp strtotime ("+ 1 day") at this time tomorrow.
Current time: echo date ("Y-m-d H: I: s", time () Result: 09:40:25
Time specified: echo date ("Y-m-d H: I: s", strtotime ("+ 1 day") Result: 09:40:25

(2) print the PHP timestamp strtotime ("-1 day") at this time yesterday.
Current time: echo date ("Y-m-d H: I: s", time () Result: 09:40:25
Time specified: echo date ("Y-m-d H: I: s", strtotime ("-1 day") Result: 09:40:25

(3) print the timestamp strtotime ("+ 1 week") for the next week.
Current time: echo date ("Y-m-d H: I: s", time () Result: 09:40:25
Time specified: echo date ("Y-m-d H: I: s", strtotime ("+ 1 week") Result: 09:40:25

(4) print the timestamp strtotime ("-1 week") for the last week.
Current time: echo date ("Y-m-d H: I: s", time () Result: 09:40:25
Time specified: echo date ("Y-m-d H: I: s", strtotime ("-1 week") Result: 09:40:25

(5) print the specified PHP timestamp strtotime ("next Thursday") for the next week.
Current time: echo date ("Y-m-d H: I: s", time () Result: 09:40:25
Time specified: echo date ("Y-m-d H: I: s", strtotime ("next Thursday") Result: 00:00:00

(6) print the timestamp strtotime ("last Thursday") of the specified last week.
Current time: echo date ("Y-m-d H: I: s", time () Result: 09:40:25
Specified Time: echo date ("Y-m-d H: I: s", strtotime ("last Thursday") Result: 00:00:00

To sum up, all the above examples of time Stamp conversion date show that the above three functions are converted to the strtotime (), date (), time () functions, if you want to know its usage, you can search for it on this site.

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.