昨天的 PHP 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6796866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP date yesterday
提问by Explosion Pills
Possible Duplicate:
Get timestamp of today and yesterday in php
可能的重复:
在 php 中获取今天和昨天的时间戳
I was wondering if there was a simple way of getting yesterday's date through this format:
我想知道是否有一种简单的方法可以通过这种格式获取昨天的日期:
date("F j, Y");
回答by KingCrunch
date()
itself is only for formatting, but it accepts a second parameter.
date()
本身仅用于格式化,但它接受第二个参数。
date("F j, Y", time() - 60 * 60 * 24);
To keep it simple I just subtract 24 hours from the unix timestamp.
为了简单起见,我只是从 unix 时间戳中减去 24 小时。
A modern oop-approach is using DateTime
现代 oop 方法正在使用 DateTime
$date = new DateTime();
$date->sub(new DateInterval('P1D'));
echo $date->format('F j, Y') . "\n";
Or in your case (more readable/obvious)
或者在您的情况下(更易读/更明显)
$date = new DateTime();
$date->add(DateInterval::createFromDateString('yesterday'));
echo $date->format('F j, Y') . "\n";
(Because DateInterval
is negative here, we must add()
it here)
(因为DateInterval
这里是否定的,所以我们必须add()
在这里)
See also: DateTime::sub()
and DateInterval
回答by Explosion Pills
strtotime()
, as in date("F j, Y", strtotime("yesterday"));
strtotime()
,如 date("F j, Y", strtotime("yesterday"));
回答by Vijay Verma
How easy :)
多么容易:)
date("F j, Y", strtotime( '-1 days' ) );
Example:
例子:
echo date("Y-m-j H:i:s", strtotime( '-1 days' ) ); // 2018-07-18 07:02:43
Output:
输出:
2018-07-17 07:02:43