Common time and date functions for formatting in PHP

Source: Internet
Author: User
Tags current time time and date

Time () function

The time () function returns the Unix timestamp of the current time. Returns the number of seconds from the Unix epoch (January 1, January 1, 1970 00:00:00 GMT) to the current time.

Since PHP 5.1, the timestamp at the TIME when the REQUEST is initiated is saved in $ _ SERVER ['request _ time.

The code is as follows: Copy code

<? Php
$ Time = time ();
Echo ($ time. "<br/> ");
Echo (date ("d f d Y", $ time ));
?>

Program running result:

1292984702

Wed December 22, 2010

The unit of 1292984702 is seconds, from 00:00:00 on January 1, January 1, 1970 to the current number of seconds.

Sometimes we want to know the situation of today in the next week, such as the number of the day, the number of the day of the week, etc. We can write this:

The code is as follows: Copy code

<? Php
$ NextWeek = time () + (7*24*60*60); // 7 days; 24 hours; 60 mins; 60 secs
Echo 'NOW: '. date ('Y-m-D'). "<br/> ";
Echo 'next Week: '. date ('Y-m-d d', $ nextWeek). "<br/> ";
?>

Program running result:

Now: 2010-12-22

Next Week: 2010-12-29 Wed

Date () function

This function is quite familiar.

The code is as follows: Copy code

<? Php
Echo ("Result with date (): <br/> ");
Echo (date ("l"). "<br/> ");
Echo (date ("l dS of f y h: I: s A"). "<br/> ");
Echo ("Oct 3,1975 was on a". date ("l", mktime (,). "<br/> ");
Echo (date (DATE_RFC822). "<br/> ");
Echo (date (DATE_ATOM, mktime (,). "<br/> ");

Echo ("Result with gmdate (): <br/> ");
Echo (gmdate ("l"). "<br/> ");
Echo (gmdate ("l dS of f y h: I: s A"). "<br/> ");
Echo ("Oct 3,1975 was on a". gmdate ("l", mktime (,). "<br/> ");
Echo (gmdate (DATE_RFC822). "<br/> ");
Echo (gmdate (DATE_ATOM, mktime (,). "<br/> ");
?>

Program running result:


Result with date ():
Wednesday
Wednesday 22nd of December 2010 02:36:18 AM
Oct 3,1975 was on a Friday
Wed, 22 Dec 10 02:36:18 + 0000
1975-10-03T00: 00: 00 + 00: 00

Result with gmdate ():
Wednesday
Wednesday 22nd of December 2010 02:36:18 AM
Oct 3,1975 was on a Friday
Wed, 22 Dec 10 02:36:18 + 0000
1975-10-03T00: 00: 00 + 00: 00

Therefore, we should give a compatibility method, use gmdate in a unified manner, and manually set the current time zone. The improvement is as follows:

The code is as follows: Copy code

Echo gmdate ('Y-m-d H: I: S', time () + 3600*8 );

In this way, both Linux + Apache and Windows get the correct results. Of course, this write method also has the advantage that when the website is for the whole world, you only need to set the time zone where the website user is located, the program automatically calculates the time based on the time zone set by the user. The database only saves the time generated by the current time (). The release time displayed in the China + 8 time zone is: 12:15:27, the release time of this information displayed by users in the European + 2 time zone is: 06:15:27, so that all the information time is correct.

Display date in a custom format for the community

The code is as follows: Copy code

/**
* Formatted display time function
*
* Similar to Douban (a few seconds ago/a few minutes ago/Today/yesterday/3-6)
*
* @ Param $ timestamp: a required timestamp parameter. The following three parameters are optional. To increase the computing speed, we recommend that you assign values directly.
* @ Param $ now CURRENT TIMESTAMP
* @ Param $ today: today's timestamp
* @ Param $ yesterday timestamp
* @ Return string
*/
Function timeshow ($ timestamp, $ now = 0, $ today = 0, $ yesterday = 0 ){
If (! $ Timestamp) {return '';}
If (! $ Now) {$ now = time ();}
If (! $ Today) {$ today = strtotime ("today ");}
If (! $ Yesterday) {$ yesterday = $ today-86400 ;}
$ Cha = $ now-$ timestamp;
If ($ cha <60 ){
Return $ cha. "seconds ago ";
} Elseif ($ cha <60*60 ){
$ Min = floor ($ cha/60 );
Return $ min. "minutes ago ";
} Elseif ($ timestamp >=$ today ){
Return date ("today H: I", $ timestamp );
} Elseif ($ timestamp >=$ yesterday ){
Return date ("yesterday H: I", $ timestamp );
} Else {
Return date ("m-d", $ timestamp );
}
}

At last, we will introduce some parameters.

Format Description:
Format Description
Y 4-digit year, and y is a two-digit number, for example, 99 is January 1, 1999.
M Number month, with a leading 0 in front, for example, 01. N is a month with no leading 0 digits
F The month in the complete text format, such as January or March.
M The month, for example, Jan or Mar.
D The day in the month, with a leading 0 in front, for example, 03. J indicates the number of days without leading 0.
W The day of the week, expressed in numbers, 0 represents Sunday
Z The day of the year, ranging from 0 to 366.
W The week of the year, for example, week 32nd.
H 24-hour format, with a leading value of 0 and h in 12-hour format
G 24-hour format, no leading 0, g corresponding to 12-hour format
I Minute format with leading 0
S Second format, with leading 0
A Upper case to lower case, for example, AM and

The optional timestamp parameter indicates the timestamp. The default value is time (), that is, the current timestamp.

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.