Jump to content

[SOLVED] Manipulating dates


Kerblam

Recommended Posts

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

$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)

 

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!

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

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.