How to find out what the date was 5 days ago?

Php

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

I think a readable way of doing that is:

$days_ago = date('Y-m-d', strtotime('-5 days', strtotime('2008-12-02')));

Solution 2 - Php

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")));

Solution 3 - Php

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";

Solution 4 - Php

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.

Solution 5 - Php

Try this

$date = date("Y-m-d", strtotime("-5 day"));

Solution 6 - Php

Simply do this...hope it works

$fifteendaysago = date_create('15 days ago');
echo date_format($fifteendaysago, 'Y-m-d');

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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

Solution 10 - Php

simple way to find the same is

$date = date("Y-m-d", strtotime('-5 days', strtotime('input_date')));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYetiView Question on Stackoverflow
Solution 1 - PhpMikeView Answer on Stackoverflow
Solution 2 - Phpmanoj singhView Answer on Stackoverflow
Solution 3 - PhpitsazzadView Answer on Stackoverflow
Solution 4 - PhpzneakView Answer on Stackoverflow
Solution 5 - PhpMattView Answer on Stackoverflow
Solution 6 - PhpKrishna JangidView Answer on Stackoverflow
Solution 7 - PhpM. S. B.View Answer on Stackoverflow
Solution 8 - PhpCalfordMathView Answer on Stackoverflow
Solution 9 - PhpPeter BaumView Answer on Stackoverflow
Solution 10 - PhpPranav MSView Answer on Stackoverflow