PDA

View Full Version : strotime + 20 years to field already submitted in form.


nanny
01-31-2010, 04:10 AM
Hi I have a form the user puts in the date for purchase I use:



if (!preg_match('{
^
(31|[123]0|[012]?[1-9]) #day
[-/.]
(0?[1-9]|1[0-2]) #month
[-/.]
([1-9][0-9]{3}|[0-9]{2}) #year
$
}x',$name8)) {
$error['name8'] = 'Please enter date as day/month/year';



I want to add 20 years to this into another field expiry and not sure how to do this. The expiry field will be a hidden field in the form and shown on the next page that shows the output of the filled out form in a certificate that shows purchase date and expiry of registration.

I have tried a number of things but it doesn't work so I was hoping someone could enlighten me.

tux
01-31-2010, 12:47 PM
Could you not use date() to automatically get the date then use it like this.

eg:

<?php

$day = date(d);
$month = date(m);
$year = date(Y);
$expiry = date(Y) + 20;

echo $day;
echo $month;
echo $year;
echo $expiry;

?>

Today this would output...

31 01 2010 2030

Just an idea!

nanny
02-03-2010, 02:16 AM
thanks - I was trying something like that but the problem lies where this is a form the customer fills out - the first field they put in the date of purchase
- the second field (hidden) is to add the 20 years to whatever the person adds into the first field and goes into the database.

edbr
02-03-2010, 02:54 AM
Please enter date as day/month/year';
can you nor explode using? as the delimiter then add 20 to the year

tux
02-03-2010, 06:59 AM
thanks - I was trying something like that but the problem lies where this is a form the customer fills out - the first field they put in the date of purchase
- the second field (hidden) is to add the 20 years to whatever the person adds into the first field and goes into the database.

Yes but to make it easy for yourself if you made the form 3 fields (day, month, year) instead of 1 field you could easily change the year without exploding it from the whole date.

You could also then be user friendly by offering drop downs for the day and month.

Is this for a purchase the user is making or has made previously. If its a purchase they are making you could get the date yourself at the point of sale using date() as I said before.

nanny
02-03-2010, 09:59 PM
Hi it is a purchase they have already made - this is a warranty registration and spits out a warranty registration certificate that they keep and goes into the database. It is international e.g. from UK to Japan to Australia to wherever the company sells the products. I thought of using calendar but that still doesn't help my problem and I thought if their purchase date was a few years ago then they might not be on the right year and select the current year and then it would be wrong details so it is easier for them to put the details themself.

The backend allows the database to be output as a CSV file and trying to learn how to make it into a pdf and sent as an email.

Thanks for your help - I could test the three separate form fields I will have to change all the backend and database to suit. I was hoping for an easy option.
Cheers

nanny
02-03-2010, 10:02 PM
Sorry - forgot to say if the form field for the purchase date is broken down into three then the CSV output would be three parts for the date - might be a problem...

tux
02-04-2010, 08:44 PM
How about this???

<?php

$date = "01/01/2010";

$year = substr("$date", 6); // returns "2010"

$expiry = $year+20;

echo $expiry;//output would be 2030
?>

nanny
02-11-2010, 02:55 AM
Hi thank you - looks good but will have no idea what year it will be in the first place. The first field is asking for them to put in the date and they have to follow the format as validated below but without knowing the year I don't see how it will calculate accurately. I have tried outputing as the mysql row but it doesn't work. I guess because it isn't entered yet.

if (!preg_match('{
^
(31|[123]0|[012]?[1-9]) #day
[-/.]
(0?[1-9]|1[0-2]) #month
[-/.]
([1-9][0-9]{3}|[0-9]{2}) #year
$
}x',$name8)) {
$error['name8'] = 'Please enter date as day/month/year';

tux
02-11-2010, 06:55 AM
$date will come from the $_POST from your form.

So, the user will enter 01/01/2010 (or whatever purchase date was) and you will collect it with...

$date = $_POST['date'];// assuming your date field is named 'date'.

You can then get the year using substr by counting the characters before the year(6) which are 01/01/ leaving the year, yes!

$year = substr("$date", 6); // returns "2010"

Then you can just add 20 to the result to get the expiry year.

$expiry = $year+20;

Does that make more sense?

tux
02-11-2010, 08:02 PM
I have taken this a bit further for you.

The following checks the users input has the required 10 characters. It also checks to see if they have entered the forward slashes correctly and corrects it for them if not.

I have commented the script so you can follow it. If you copy it to a new page and run it you will see it outputs the day, month & year the expiry year and the full date entered by the user. You can use these results then how you like. The second $date var is added just so the script has something to work with, you can delete it later.

Heres the script....

<?php
//////////////////////////////////////////////////delclare our vars
$date = $_POST['date']; //from your form assuming your form field is named 'date'
$date = "11;02:2010"; //we will use this for now to test remove it later. lets say the user enters the wrong characters not slashes
$date_length = strlen($date); //how many characters in date should be 10
$search = array("-","<",">",";",":"); //characters we don't want to see, you can add others
$slash = "/"; //we will replace them with a slash
//////////////////////////////////////////////////

//////////////////////////////////////////////////
if($date_length == 10){ //if the date has 10 characters we run the script

$date_format = str_replace($search, $slash, $date); //replace the dashes with slashes if present

$day = substr("$date_format",0, -8); //return the day, starting at character zero and finishing at character 2(-8)
$month = substr("$date_format", 3, -5); //return the month, starting at character 3 and finishing at character 5(-5)
$year = substr("$date_format", 6); //return the year, starting at character 6

$expiry = $year+20; //add 20 to the year to get expiry
//////////////////////////////////////////////////

/////////////////////////////our results
echo $day; //we have the day to use as we wish
echo "<br>".$month; //we have the month to use as we wish
echo "<br>".$year; //we have the year to use as we wish

echo "<br>".$expiry; //the expiry year, output would be 2030

echo "<br>".$date_format; //the full date
/////////////////////////////

/////////////////////////////

}elseif(($date_length <= 9) ||($date_length >=11)){ //if too few or too many characters we throw the error

echo "Sorry you entered the date incorrectly, please use this format: DD/MM/YYYY";

}

?>

Hope it helps.

Paul

nanny
02-16-2010, 07:06 AM
Thanks I will give it a try and let you know....