How to find out what the date was 5 days ago?
Well, the following returns what date was 5 days ago:
$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?
For example: What was 5 days prior to 2008-12-02?
I think a readable way of doing that is:
$days_ago = date('Y-m-d', strtotime('-5 days', strtotime('2008-12-02')));
find out what the date was 5 days ago from today in php
$date = strtotime(date("Y-m-d", strtotime("-5 day")));
find out what the date was n days ago from today in php
$date = strtotime(date("Y-m-d", strtotime("-n day")));
5 days ago from a particular date:
$date = new DateTime('2008-12-02');
$date->sub(new DateInterval('P5D'));
echo $date->format('Y-m-d') . "\n";
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:
$days_ago = date('Y-m-d', strtotime('January 18, 2034') - 5 * SECONDS_PER_DAY);
Or, as you used, 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.
Simply do this...hope it works
$fifteendaysago = date_create('15 days ago');
echo date_format($fifteendaysago, 'Y-m-d');
Try this
$date = date("Y-m-d", strtotime("-5 day"));
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
Use the built in date_sub and date_add functions to math with dates. (See http://php.net/manual/en/datetime.sub.php)
Similar to Sazzad's answer, but in procedural style 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
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
simple way to find the same is
$date = date("Y-m-d", strtotime('-5 days', strtotime('input_date')));
精彩评论