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!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Ok sorry for the post on some seemingly easy to understand functions but i searched google and this forum for those keywords and couldnt find what im looking for.
Anyways on to the question. lets say i have a string of number 01012001 how would i convert this to a time format of 01 - 01 - 2001?
I have a function that probably is complete bogus but im trying to double check a users age on a payment form. Not really necessary cuz the credit card validation process will do that but i wanted to try and cut down on the errors in the form before it gets set to a cc server for validation. Hopefully trying to cut down on bandwidth used up just by simple mistakes in filling out the form. So im trying to verify the age is at least 18.
I want to make sure it is in the proper format of 01-01-2001 which i dont understand as of yet.
And output in the correct format as well. So far this is my best shot at it:
<?php
function DOB ($m, $d, $y) {
//Convert Date of Birth to String
$d = $_POST['BillD'];
$m = $_POST['BillM'];
$y = $_POST['BillY'];
$dob = $m . $d . $y;
//convert currnt time to string -18
$currD =date('d');
$currM =date('m');
$currY =date('Y');
$dobchk = $currM . $currD . ($currY - 18);
//Check to see if DOB is less than or equal to DOBCHK
if ($dob <= $dobchk) {
if ($frmt = date('m - d - Y', strtotime($dob, $frmt))) {
return $dob; }
}else { return false; }
}
?>
Plz take into consideration i am very knew and dont really know what im doing...so try not to blast me too hard on this one. If this idea is completely bogus is there another way to do this? Or am i missing the point in here somewhere?
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
use sbstr() to format it to "01 01 2001"
then use mktime() to format that into a timestamp?
I thought of using that but couldnt quite get it.
Anyways i found a loop whole in my logic and had to change the function anyways.
now all is good. Well as good as my rudementarty skills with PHP will allow it to be.
I will have to keep in mind your solution tho encase it ever comes up in the future
And sorry about submitting my code wrong i didnt notice the php tag. My bad. Will use proper syntax for now on.
<?php
function DOB ($m, $d, $y) {
$d = $_POST['BillD'];
$m = $_POST['BillM'];
$y = $_POST['BillY'];
$ts = mktime(0, 0, 0, $m, $d, $y)
$time_gap = time() - $ts;
if ( $time_gap < 567648000 ) // span between now and posted date less than 18 years
{
return false;
}
else
{
// Return the DOB in m/d/YYYY format
return date('m/d/Y', $ts);
}
}
?>
@everah youre not only my moddy but right now id call you my daddy if that didnt sound so intensely gay.
I wanted to do a time_gap but was never sure how the time() function actually worked.
567648000 and i really havent a clue how you got that little number and i was pretty sure thats the peice i was missing to make
my function errrh....uh....function.
So you are the man!!!
I hope you dont mind but im stealing that snippet and if you do mind i can put your name on it.
the formula should be 60 * 60 * 24 * 365.25 * 18 = 568021248.
the calender we use is actually flawed and that is why we have a leap year every 4 yrs. Thats why you have to up in the ".25" to account for that extra quarter of a day we have each year.
Im no expert at PHP but when i did this it turned out to be spot on.
thanks again for the snippet of code.
Feyd-> Glad to see i was wrong once again. LOL. Thanks for correcting me on the 365.25 deal. I had no idea what the real scientific value was i was just taking a stab in the dark as to why the number everah gave me was off by four days. Honestly it took me like a half hour to figure out where and how to get the numbers i did.
I know the php manual prolly explains it but i really hate that site.
Everah-> Thanks for cleaning up that bit of code of for me. Although im still unsure what the "?" marks are used for in PHP.
I realized the mktime() wont read any dates before 1970. And since that puts me at a limit of what 36yr olds i think i will be resorting back to my former method.
Gonna do some research on the ?'s now. thanks for all the help