Common formatting display time-date functions in PHP _php tutorial

Source: Internet
Author: User
The date function in PHP can be implemented in the format of dates and time display, this is also a few examples we commonly used, the following to introduce to you friends, I hope this tutorial is helpful.

Time () function

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

The timestamp of the time the request was initiated was saved in $_server[' Request_time ' from PHP 5.1.

The code is as follows Copy Code

$time = time ();
Echo ($time. "
");
Echo (Date ("D F D Y", $time));
?>

Program Run Result:

1292984702

Wed December 22 2010

1292984702 units are seconds, from January 1, 1970 00:00:00 to the current number of seconds.

Sometimes we want to know what's going on in the next week, such as the number of days, the day of the week, and so on, we can write:

The code is as follows Copy Code

$nextWeek = time () + (7 * 24 * 60 * 60); 7 days; Hours; mins; 60secs
Echo ' Now: '. Date (' y-m-d '). "
";
Echo ' Next Week: '. Date (' y-m-d d ', $nextWeek). "
";
?>

Program Run Result:

Now:2010-12-22

Next week:2010-12-29 Wed

Date () function

This function is more familiar.

The code is as follows Copy Code

Echo ("Result with date ():
");
Echo (Date ("L"). "
");
Echo (Date ("L DS of F Y h:i:s A"). "
");
Echo ("Oct 3,1975 is on a". Date ("L", Mktime (0,0,0,10,3,1975)). "
");
Echo (Date (date_rfc822). "
");
Echo (Date (Date_atom,mktime (0,0,0,10,3,1975)). "

");

Echo ("Result with Gmdate ():
");
Echo (Gmdate ("L"). "
");
Echo (Gmdate ("L DS of F Y h:i:s A"). "
");
Echo ("Oct 3,1975 is on a". Gmdate ("L", Mktime (0,0,0,10,3,1975)). "
");
Echo (Gmdate (date_rfc822). "
");
Echo (Gmdate (Date_atom,mktime (0,0,0,10,3,1975)). "
");
?>

Program Run Result:


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

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

Therefore, we should give a compatibility of the wording, unified use of gmdate, and manually set the current time zone, the following improvements:

The code is as follows Copy Code

echo gmdate (' y-m-d h:i:s ', time () + 3600 * 8);

So no matter under the Linux+apache or windows are getting the correct results, of course, there is a benefit to write, when the site is facing the world, then the site users as long as the settings in the time zone, the program automatically based on the time zone set by the user to calculate, The publication time of the information in the database is only the time that is generated by the current date (), then the published time in China + 8 time zone is: 2007-03-14 12:15:27, then in Europe + 2 time zone users see this information published: 2007-03-14 06:15:27, the time for this information is all right.

A custom format for the community to display the date

The code is as follows Copy Code

/**
* Formatted display of time functions
*
* Similar watercress (a few seconds ago/a few minutes ago/today 3:50/yesterday 3:50/3-6)
*
* @param $timestamp Timestamp required parameter, the following three parameters are optional, in order to improve the speed of calculation, it is recommended to directly assign value
* @param $now Current timestamp
* @param $today today 00:00 time stamp
* @param $yesterday yesterday 00:00 time stamp
* @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 ("H:i Today", $timestamp);
}elseif ($timestamp >= $yesterday) {
Return date ("Yesterday H:i", $timestamp);
}else{
Return date ("m-d", $timestamp);
}
}

At the end of this article, we introduce some parameters

Description of format mode:
Formatting Methods Description
Y 4-digit year, Y is a 2-digit number, e.g. 99 is 1999
M A numeric month preceded by a leading 0, such as 01. N is no leading 0 digit month
F month, full text format, such as January or March
M A three-letter abbreviation of the month, such as Jan or Mar
D The day ordinal of the month preceded by a leading 0, such as 03. J is a number of days without leading 0
W Day of the week, expressed as a number, 0 means Sunday
Z The day ordinal of the year, range 0-366
W The first week of the year, such as the 32nd week
H 24-hour format with leading 0,h for 12-hour format
G 24-hour format, no preamble 0,g for corresponding 12-hour format
I Minute format, with leading 0
S Seconds format, with leading 0
A Capitalize on the afternoon, such as am,a for lowercase

An optional parameter, timestamp, represents a timestamp, which defaults to time (), which is the current timestamp.

http://www.bkjia.com/PHPjc/632731.html www.bkjia.com true http://www.bkjia.com/PHPjc/632731.html techarticle The date function in PHP can be implemented in the format of dates and time display, this is also a few examples we commonly used, the following to introduce to you friends, I hope this tutorial is helpful. ...

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