Simple Date Prob - Help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hudson
Forum Newbie
Posts: 3
Joined: Tue Apr 30, 2002 5:49 am

Simple Date Prob - Help!!

Post 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
pozytron
Forum Newbie
Posts: 12
Joined: Fri May 03, 2002 7:14 pm
Location: Denver

Post 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)));
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
pozytron
Forum Newbie
Posts: 12
Joined: Fri May 03, 2002 7:14 pm
Location: Denver

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Apologies pozytron, I should have tested your code first :oops:

Mike
pozytron
Forum Newbie
Posts: 12
Joined: Fri May 03, 2002 7:14 pm
Location: Denver

Post by pozytron »

Not a problem, mike. Actually, your method is much more short and elegant, and I applaud you for that.

Cheers
Post Reply