search
Homephp教程php手册PHP日期时间格式化date()与gmdate()用法

date/time 函数允许您提取并格式化服务器上的日期和时间。这些函数依赖于服务器的本地设置。

先介绍下time()函数:

time()函数

time() 函数返回当前时间的 Unix 时间戳。返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。

自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了发起该请求时刻的时间戳。

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

程序运行结果:

1292984702
Wed December 22 2010

1292984702单位是秒,从1970 年 1 月 1 日 00:00:00到现在的秒数。

有时候我们想知道下一周的今天的一些情况,比如是几号,星期几等等,我们可以这么写:

<?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 />";
?>

程序运行结果:

Now: 2010-12-22
Next Week: 2010-12-29 Wed

date()函数

这个函数就比较熟悉了。

<?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 />");
?>

程序运行结果:

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

所以,我们应该给一个兼容性的写法,统一使用gmdate,并手工设置当前时区,写法改进如下:

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

这样不管在Linux+Apache下还是Windows下都得到了正确的结果,当然这样写还有一个好处,当网站是面向全世界的时候,那么网站用户只要设置所在的时区,程序自动根据用户设置的时区进行时间计算,数据库中信息发布时间只存当前的time()所生成的时间,那么在中国+8时区看到的发布时间是:2007-03-14 12:15:27,那么在欧洲+2时区用户看到这个信息的发布时间是:2007-03-14 06:15:27,这样信息的时间就全部对应正确了。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)