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天是什么时候?
8 个解决方案
#1
90
I think a readable way of doing that is:
我认为这样做的可读方式是:
$days_ago = date('Y-m-d', strtotime('-5 days', strtotime('2008-12-02')));
#2
9
define('SECONDS_PER_DAY', 86400);
$days_ago = date('Y-m-d', time() - 5 * SECONDS_PER_DAY);
Other than that, you can use strtotime
for 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.
好吧,你明白了。关键是从时间戳中删除足够的秒数。
#3
9
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")));
#4
4
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";
#5
1
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.htm or http://en.wikipedia.org/wiki/Julian_day
如果你想要一个你知道算法的方法,或者上一个答案中提到的函数不可用:将日期转换为朱利安日数(这是从公元前4713年1月1日开始计算天数的方法),然后减去五,然后转换回日历日期(年,月,日)。这两个转换算法的来源是http://www.hermetic.ch/cal_stud/jdn.htm的第9节或http://en.wikipedia.org/wiki/Julian_day
#6
0
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
#7
0
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
日期操作的一般算法将日期转换为Julian Day Numbers和从Julian Day Numbers转换日期。以下是此类算法描述的链接,当前已知最佳算法的说明以及每种算法的数学证明:http://web.archive.org/web/20140910060704/http://mysite.verizon .NET / aesir_research /日期/ date0.htm
#8
0
Try this
尝试这个
$date = date("Y-m-d", strtotime("-5 day"));
#1
90
I think a readable way of doing that is:
我认为这样做的可读方式是:
$days_ago = date('Y-m-d', strtotime('-5 days', strtotime('2008-12-02')));
#2
9
define('SECONDS_PER_DAY', 86400);
$days_ago = date('Y-m-d', time() - 5 * SECONDS_PER_DAY);
Other than that, you can use strtotime
for 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.
好吧,你明白了。关键是从时间戳中删除足够的秒数。
#3
9
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")));
#4
4
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";
#5
1
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.htm or http://en.wikipedia.org/wiki/Julian_day
如果你想要一个你知道算法的方法,或者上一个答案中提到的函数不可用:将日期转换为朱利安日数(这是从公元前4713年1月1日开始计算天数的方法),然后减去五,然后转换回日历日期(年,月,日)。这两个转换算法的来源是http://www.hermetic.ch/cal_stud/jdn.htm的第9节或http://en.wikipedia.org/wiki/Julian_day
#6
0
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
#7
0
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
日期操作的一般算法将日期转换为Julian Day Numbers和从Julian Day Numbers转换日期。以下是此类算法描述的链接,当前已知最佳算法的说明以及每种算法的数学证明:http://web.archive.org/web/20140910060704/http://mysite.verizon .NET / aesir_research /日期/ date0.htm
#8
0
Try this
尝试这个
$date = date("Y-m-d", strtotime("-5 day"));