Format date () and gmdate () in PHP. The datetime function allows you to extract and format the date and time on the server. These functions depend on the local settings of the server. First, we will introduce the time () function: the time () function returns the date/time function, allowing you to extract and format the date and time on the server. These functions depend on the local settings of the server.
First, we will introduce the time () function:
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.
");echo(date("D F d Y", $time));?>
Program running result:
1292984702Wed 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:
";echo 'Next Week: '. date('Y-m-d D', $nextWeek) ."
";?>
Program running result:
Now: 2010-12-22Next Week: 2010-12-29 Wed
Date () function
This function is quite familiar.
");echo(date("l") . "
");echo(date("l dS of F Y h:i:s A") . "
");echo("Oct 3,1975 was 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 was 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 running result:
Result with date():WednesdayWednesday 22nd of December 2010 02:36:18 AMOct 3,1975 was on a FridayWed, 22 Dec 10 02:36:18 +00001975-10-03T00:00:00+00:00Result with gmdate():WednesdayWednesday 22nd of December 2010 02:36:18 AMOct 3,1975 was on a FridayWed, 22 Dec 10 02:36:18 +00001975-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:
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.
The http://www.bkjia.com/PHPjc/752563.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752563.htmlTechArticledate/time function allows you to extract and format dates and times on the server. These functions depend on the local settings of the server. First introduce the time () function: time () function return...