PHP getdate() 与 date()

发布于 2024-10-31 16:52:26 字数 422 浏览 1 评论 0原文

理论问题。

想象一下情况。我需要获取今天的日期和时间(不是现在,而是今天——一天的开始)。我可以使用以下代码来完成此操作:

$now = time();
$today = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m", $now), date("d", $now), date("Y", $now)));

或这样:

$now = getdate();
$today = date('Y-m-d H:i:s', mktime(0, 0, 0, $now['mon'], $now['mday'], $now['year']));

在我见过的大多数示例中,使用第一种方法。问题很简单:为什么?第一个使用了 3 个函数调用来获取月、日和年。

Theoretical question.

Imagine the situation. I need to get today date and time (not now, but today - the start of the day). I can do it with either this code:

$now = time();
$today = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m", $now), date("d", $now), date("Y", $now)));

or this:

$now = getdate();
$today = date('Y-m-d H:i:s', mktime(0, 0, 0, $now['mon'], $now['mday'], $now['year']));

In most examples I've seen, the first way is used. The question is simple: why? The first one uses 3 function calls more to get month, day and year.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2024-11-07 16:52:26

这两个选项都非常可怕 - 如果您试图以格式化字符串的形式获取午夜的当前日期,则很简单:

date('Y-m-d') . ' 00:00:00';

或者,如果您想更明确一点,则

date('Y-m-d H:i:s', strtotime('today midnight'));

无需这样做>mktime 的事情。编写该代码的人不知道自己在做什么和/或者是复制粘贴/货物崇拜开发人员。如果您确实在“大多数示例”中看到了这一点,那么与您一起出去玩的人群深感困扰,您可能应该停止与他们一起出去玩。

mktime 唯一有趣的事情是尝试使用本地时区。如果您的工作对时区敏感,并且您使用的是 PHP 5.3 或更高版本,请考虑使用 DateTimeDateTimeZone 代替。 PHP交互提示的演示:(

php > $utc = new DateTimeZone('UTC');
php > $pdt = new DateTimeZone('America/Los_Angeles');
php > $midnight_utc = new DateTime('today midnight', $utc);
php > $midnight_utc->setTimeZone($pdt);
php > echo $midnight_utc->format('Y-m-d H:i:s');
2011-04-08 17:00:00

目前UTC为第9,PDT为第8。)

Both of those options are pretty horrible -- if you're trying to get the current date at midnight as a formatted string, it's as simple as:

date('Y-m-d') . ' 00:00:00';

Or, if you want to be slightly more explicit,

date('Y-m-d H:i:s', strtotime('today midnight'));

No need to do that wacky mktime thing. Whoever wrote that code does not know what they are doing and/or is a copy-paste/cargo-cult developer. If you really see that in "most examples," then the crowd you're hanging out with is deeply troubled and you should probably stop hanging out with them.

The only interesting thing that mktime does is attempt to work with the local timezone. If your work is timezone sensitive, and you're working with PHP 5.3 or better, consider working with DateTime and DateTimeZone instead. A demo from the PHP interactive prompt:

php > $utc = new DateTimeZone('UTC');
php > $pdt = new DateTimeZone('America/Los_Angeles');
php > $midnight_utc = new DateTime('today midnight', $utc);
php > $midnight_utc->setTimeZone($pdt);
php > echo $midnight_utc->format('Y-m-d H:i:s');
2011-04-08 17:00:00

(At the moment, it is the 9th in UTC, while it's the 8th in PDT.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文