PHP日期昨天 [英] PHP date yesterday
问题描述
可能重复:
今天和昨天在php 获取时间戳
Possible Duplicate:
Get timestamp of today and yesterday in php
$ b $我想知道是否有一种简单的方式通过这种格式获取昨天的日期:
I was wondering if there was a simple way of getting yesterday's date through this format:
date("F j, Y");
推荐答案
date()
本身仅用于格式化,但它接受第二个参数。
date()
itself is only for formatting, but it accepts a second parameter.
date("F j, Y", time() - 60 * 60 * 24);
为了保持简单,我只需从unix时间戳中减去24小时。
To keep it simple I just subtract 24 hours from the unix timestamp.
现代oop方法使用 DateTime
A modern oop-approach is using 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";
(因为 DateInterval
必须 add()
在这里)
(Because DateInterval
is negative here, we must add()
it here)
另请参见: DateTime :: sub()
和 DateInterval
See also: DateTime::sub()
and DateInterval
这篇关于PHP日期昨天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!