Page 1 of 1

Simple Date Prob - Help!!

Posted: Fri May 03, 2002 8:23 pm
by hudson
ok i just need a little help with this date shiot

i have a form that takes user input for billing periods .. aka start and end

so i end up with 2 vars that look like this

$billstart = $billsinput // a date formated like 2002-05-01
$billend = $billeinput // 2002-05-31

now wut i need to do is to be able to take $billend and add 15 days to it for a dudate giving me a valid date of something like 2002-06-15.

so basically i just want to always have 15 days added to a user inputed $billend and have it automagically adjust whether it needs to bump the month and stuff depending on the given $billend

any help/examples would be greatly appreciated
thanks in advance..
Hud

Posted: Fri May 03, 2002 9:12 pm
by pozytron
OK, this is a pretty easy thing to do in php, weakly typed as it is. just use the following:

Code: Select all

$duedate = strftime("%Y-%m-%d",mktime(0,0,0,substr($billend,5,2),substr($billend,8,2)+15,substr($billend,0,4)));

Posted: Sat May 04, 2002 4:00 am
by mikeq

Code: Select all

$duedate = strftime("%Y-%m-%d",mktime(0,0,0,substr($billend,5,2),substr($billend,8,2)+15,substr($billend,0,4)));
Would that not end up trying to give dates like 2002-03-51 if you just add 15 to the day component, therefore generating an error?

What about

Code: Select all

$EndAsTimeStamp = StrToTime($billeinput);
//get 15 days as seconds, you could define this as a constant
$NoSeconds = 15*24*60*60;
$DueAsTimeStamp = $EndAsTimeStamp + $NoSeconds;
$DueDate = date('Y-m-d',$DueAsTimeStamp);
or the shorter

Code: Select all

$DueDate = date('Y-m-d',StrToTime($billeinput)+(15*24*60*60));
Mike

Posted: Sat May 04, 2002 1:18 pm
by pozytron
Actually, the way I coded it, the function will not generate invalid dates, assuming you aren't using huge numbers. There are many different valid solutions to your problem. I have coded a sample page to demonstrate how these work. Source code is below. http://www.positronicsoftware.com/test.php

Code: Select all

$billend="2002-05-03";
echo $billend.':<br>';
$duedate = strftime("%Y-%m-%d",mktime(0,0,0,substr($billend,5,2),substr($billend,8,2)+15,substr($billend,0,4)));
echo $duedate;
$duedate = date('Y-m-d',StrToTime($billend)+(15*24*60*60));
echo '<br>'.$duedate.'<p>';

$billend="2002-05-23";
echo $billend.':<br>';
$duedate = strftime("%Y-%m-%d",mktime(0,0,0,substr($billend,5,2),substr($billend,8,2)+15,substr($billend,0,4)));
echo $duedate;
$duedate = date('Y-m-d',StrToTime($billend)+(15*24*60*60));
echo '<br>'.$duedate.'<p>';
If you can understand these examples, you'll see that both methods work equally well.

Posted: Sat May 04, 2002 1:48 pm
by mikeq
Apologies pozytron, I should have tested your code first :oops:

Mike

Posted: Sat May 04, 2002 1:50 pm
by pozytron
Not a problem, mike. Actually, your method is much more short and elegant, and I applaud you for that.

Cheers