php 如何找出5天前的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2708894/
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
How to find out what the date was 5 days ago?
提问by Yeti
Well, the following returns what date was 5 days ago:
好吧,以下返回 5 天前的日期:
$days_ago = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - 5, date("Y")));
But, how do I find what was 5 days ago from any date, not just today?
但是,我如何从任何日期(而不仅仅是今天)找到 5 天前的内容?
For example: What was 5 days prior to 2008-12-02?
例如:2008-12-02 之前 5 天是什么时候?
回答by Mike
I think a readable way of doing that is:
我认为这样做的可读方式是:
$days_ago = date('Y-m-d', strtotime('-5 days', strtotime('2008-12-02')));
回答by manoj singh
find out what the date was 5 days ago from today in php
在php中找出从今天起5天前的日期
$date = strtotime(date("Y-m-d", strtotime("-5 day")));
find out what the date was n days ago from today in php
从今天起在 php 中找出 n 天前的日期
$date = strtotime(date("Y-m-d", strtotime("-n day")));
回答by zneak
define('SECONDS_PER_DAY', 86400);
$days_ago = date('Y-m-d', time() - 5 * SECONDS_PER_DAY);
Other than that, you can use strtotimefor any date:
除此之外,您可以strtotime用于任何日期:
$days_ago = date('Y-m-d', strtotime('January 18, 2034') - 5 * SECONDS_PER_DAY);
Or, as you used, mktime:
或者,正如您所使用的,mktime:
$days_ago = date('Y-m-d', mktime(0, 0, 0, 12, 2, 2008) - 5 * SECONDS_PER_DAY);
Well, you get it. The key is to remove enough seconds from the timestamp.
好吧,你明白了。关键是从时间戳中删除足够的秒数。
回答by itsazzad
5 days ago from a particular date:
从特定日期起 5 天前:
$date = new DateTime('2008-12-02');
$date->sub(new DateInterval('P5D'));
echo $date->format('Y-m-d') . "\n";
回答by Matt
Try this
尝试这个
$date = date("Y-m-d", strtotime("-5 day"));
回答by Krishna Kumar Jangid
Simply do this...hope it help
只需这样做...希望它有所帮助
$fifteendaysago = date_create('15 days ago');
echo date_format($fifteendaysago, 'Y-m-d');
回答by M. S. B.
If you want a method in which you know the algorithm, or the functions mentioned in the previous answer aren't available: convert the date to Julian Day number (which is a way of counting days from January 1st, 4713 B.C), then subtract five, then convert back to calendar date (year, month, day). Sources of the algorithms for the two conversions is section 9 of http://www.hermetic.ch/cal_stud/jdn.htmor http://en.wikipedia.org/wiki/Julian_day
如果您想要一种知道算法的方法,或者上一个答案中提到的函数不可用:将日期转换为儒略日数(这是一种从公元前 4713 年 1 月 1 日开始计算天数的方法),然后减去五、再转换回日历日期(年、月、日)。两次转换的算法来源是http://www.hermetic.ch/cal_stud/jdn.htm或http://en.wikipedia.org/wiki/Julian_day 的第 9 节
回答by Pranav MS
simple way to find the same is
找到相同的简单方法是
$date = date("Y-m-d", strtotime('-5 days', strtotime('input_date')));
回答by CalfordMath
Use the built in date_sub and date_add functions to math with dates. (See http://php.net/manual/en/datetime.sub.php)
使用内置的 date_sub 和 date_add 函数对日期进行数学运算。(见http://php.net/manual/en/datetime.sub.php)
Similar to Sazzad's answer, but in procedural style PHP,
类似于 Sazzad 的回答,但采用 PHP 程序风格,
$date = date_create('2008-12-02');
date_sub($date, date_interval_create_from_date_string('5 days'));
echo date_format($date, 'Y-m-d'); //outputs 2008-11-27
回答by Peter Baum
General algorithms for date manipulation convert dates to and from Julian Day Numbers. Here is a link to a description of such algorithms, a description of the best algorithms currently known, and the mathematical proofs of each of them: http://web.archive.org/web/20140910060704/http://mysite.verizon.net/aesir_research/date/date0.htm
日期操作的通用算法将日期与儒略日数相互转换。以下是此类算法的描述、目前已知的最佳算法的描述以及每种算法的数学证明的链接:http://web.archive.org/web/20140910060704/http: //mysite.verizon .net/aesir_research/date/date0.htm

