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\")));
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 help
$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
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";
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