Jump to content

[SOLVED] subtracting date's


nathanmaxsonadil

Recommended Posts

You need to first convert your date into a timestamp, then take off the required number of seconds, then convert your timestamp back to whatever format you wanted. Try:

 

<?php
$date = '2007-10-05';
$timestamp = strtotime($date);
$new_timestamp = $timestamp - 60*60*24*5;
$new_date = date('Y-m-d',$new_timestamp);
echo $new_date;
?>

or...

 

$date = '2007-10-05';
$parts = explode("-",$date);
$fivedaysago = mktime(0,0,0,$parts[1],$parts[2] - 5, $parts[0]); // as timestamp
$fivedaysago2 = date("Y-m-d",$fivedaysago); // As 'YYYY-MM-DD'

 

True, though i personally avoid using the mktime function on principle. I dont know of another function where the parameter order is so completely unintuitive!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.