nathanmaxsonadil Posted October 5, 2007 Share Posted October 5, 2007 I was wondering how to subtract a date(like I have 2007-10-05 and I want it to subtract 5 days)? Link to comment https://forums.phpfreaks.com/topic/71999-solved-subtracting-dates/ Share on other sites More sharing options...
GingerRobot Posted October 5, 2007 Share Posted October 5, 2007 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; ?> Link to comment https://forums.phpfreaks.com/topic/71999-solved-subtracting-dates/#findComment-362710 Share on other sites More sharing options...
nathanmaxsonadil Posted October 5, 2007 Author Share Posted October 5, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/71999-solved-subtracting-dates/#findComment-362715 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 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' Link to comment https://forums.phpfreaks.com/topic/71999-solved-subtracting-dates/#findComment-362716 Share on other sites More sharing options...
GingerRobot Posted October 5, 2007 Share Posted October 5, 2007 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! Link to comment https://forums.phpfreaks.com/topic/71999-solved-subtracting-dates/#findComment-362723 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 yep, i had to look up the order for month, day, year. i like mktime more for the month and year math capabilities. mktime helps prevent me worrying about whether yesterday was a leap day or a daylight savings time change... Link to comment https://forums.phpfreaks.com/topic/71999-solved-subtracting-dates/#findComment-362744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.