Kerblam Posted March 24, 2007 Share Posted March 24, 2007 Hey guys, I've never had to use date() or strtotime() before and I find the help pages on php.net really confusing. I need to take the input of start day, start month, and start year, then add a number of days onto that to output a future date. $startday = '01'; $startmonth = 'July'; $startyear = '2007'; $startdate = $startday.' '.$startmonth.' '.$startyear; # So $startdate = 01 July 2007 $length = 5; $startdate = date('d/m/Y',strtotime($startdate)); # So now $startdate = 01/07/2007 (the format I want) How do I go about adding $length number of days onto $startdate, to output the end date in the d/m/Y format? Link to comment https://forums.phpfreaks.com/topic/44149-solved-manipulating-dates/ Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 $stdate = date('d/m/Y',strtotime($startdate)); you should not update a variable onto itself if you need it again // So now $stdate = 01/07/2007 (the format I want) $updateDate = strtotime("+$length day", $startdate); // So now $stdate = 06/07/2007 (the format I want) Link to comment https://forums.phpfreaks.com/topic/44149-solved-manipulating-dates/#findComment-214381 Share on other sites More sharing options...
Kerblam Posted March 24, 2007 Author Share Posted March 24, 2007 paul2463 - Thanks for your reply. However, your code doesnt return the end date in the d/m/Y format. I tried manipulating it like this: $updateDate = date('d/m/Y',strtotime("+$length day", $startdate)); However it comes out with 06/01/1970. It appears to be ignoring the timestamp value completely, and adding $length days to January 1 1970 00:00:00 GMT by default! Link to comment https://forums.phpfreaks.com/topic/44149-solved-manipulating-dates/#findComment-214395 Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 ok here we go then $strdate = strtotime($startdate);// just get a timestamp of your startdate $stdate = date('d/m/Y',strtotime($strdate)); //So now $stdate = 01/07/2007 (the format you want) $length = 5; $incdate = strtotime("+$length day", $strdate);// add the $length onto it $updateDate = date('d/m/Y',$incdate); // create the date you want echo $updateDate; //print it out Link to comment https://forums.phpfreaks.com/topic/44149-solved-manipulating-dates/#findComment-214409 Share on other sites More sharing options...
Kerblam Posted March 24, 2007 Author Share Posted March 24, 2007 Thanks a lot paul! I had to edit it slightly to integrate it into my script, but it's working great now. Cheers. Link to comment https://forums.phpfreaks.com/topic/44149-solved-manipulating-dates/#findComment-214413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.