PHP date and time formatting date() and gmdate() usage_PHP tutorial

WBOY
Release: 2016-07-13 10:32:59
Original
922 people have browsed it

The date/time functions allow you to extract and format the date and time on the server. These functions rely on the local settings of the server.

First introduce the time() function:

time() function

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

Since PHP 5.1, the timestamp of the time when the request was initiated is saved in $_SERVER['REQUEST_TIME'].

<?php
$time = time();
echo($time . "<br />");
echo(date("D F d Y", $time));
?>
Copy after login

Program execution result:

1292984702
Wed December 22 2010
Copy after login

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

Sometimes we want to know something about today in the next week, such as what day it is, what day of the week it is, etc. We can write like this:

<?php
$nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ."<br />";
echo 'Next Week: '. date('Y-m-d D', $nextWeek) ."<br />";
?>
Copy after login

Program execution result:

Now: 2010-12-22
Next Week: 2010-12-29 Wed
Copy after login

date() function

This function is relatively familiar.

<?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(0,0,0,10,3,1975))."<br />");
echo(date(DATE_RFC822) . "<br />");
echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br /><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(0,0,0,10,3,1975))."<br />");
echo(gmdate(DATE_RFC822) . "<br />");
echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br />");
?>
Copy after login

Program execution 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
Copy after login

Therefore, we should give a compatible writing method, use gmdate uniformly, and set the current time zone manually. The writing method is improved as follows:

echo gmdate('Y-m-d H:i:s', time() + 3600 * 8);
Copy after login

In this way, the correct results are obtained regardless of whether it is under Linux+Apache or Windows. Of course, there is another advantage of writing this way. When the website is for the whole world, the website user only needs to set the time zone, and the program will automatically adjust the time zone according to the user's location. The set time zone is used for time calculation. The information release time in the database only stores the time generated by the current time(). Then the release time seen in China +8 time zone is: 2007-03-14 12:15:27, then in Users in the European +2 time zone will see that the release time of this information is: 2007-03-14 06:15:27, so that all the times of the information will be correct.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752563.htmlTechArticleThe date/time function allows you to extract and format the date and time on the server. These functions rely on the local settings of the server. Let’s first introduce the time() function: time() function time() function returns...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template