strtotime and strftime help plz

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
sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

strtotime and strftime help plz

Post by sansoo »

feyd | Please use

Code: Select all

,

Code: Select all

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:

Code: Select all

<?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?


feyd | Please use

Code: Select all

,

Code: Select all

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

Post by sansoo »

I think i get what youre saying.

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are grabbing the date in parts (as the function states) so why not do this..

Code: Select all

<?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); 
    }
}
?>
sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

Post by sansoo »

@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.
sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

Post by sansoo »

incase you were wondering this is how i solved it.
Not quite as elegant but it did the trick.

Code: Select all

function DOB ($m, $d, $y) {

$d = $_POST['BillD'];
$m = $_POST['BillM'];
$y = $_POST['BillY'];


$currD =date('d');
$currM =date('m');
$currY =date('Y');


$false = "Invalid Date of Birth";
if(!empty($m) && !empty($d) && !empty($y) && strlen($m . $d . $y) ==8) {
	if($y ==($currY - 18) && $m <= $currM && $d <= $currD) {
	echo "" .$m.'-'.$d.'-'.$y.""; 
		}elseif ($y < 1988) {
		echo "" .$m.'-'.$d.'-'.$y."";  
		}else {
		return $false; }
	}else {
	return $false; }
I like to pat myself on the back for this wonderful hack atempt at PHP :lol:
sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

Post by sansoo »

Everah:

I figured out how you got that number: 567648000

but it is flawed by 4 days.

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

365.242199 is more accurate and is the length used in current calculations by many systems.

568024668 is Google's answer to 18 years in seconds.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're welcome for the code. Steal away... I cleaned your solution up a bit as well. I guess I just feel like coding right now.

Code: Select all

<?php
function DOB ($m, $d, $y) {
    $d = ( isset($_POST['BillD']) ) ? $_POST['BillD'] : false;
    $m = ( isset($_POST['BillM']) ) ? $_POST['BillM'] : false;
    $y = ( isset($_POST['BillY']) ) ? $_POST['BillY'] : false; // Set the billy is billy aint set, yeah billy

    $msg = 'Invalid Date of Birth';
    if ($m && $d && $y && strlen($m . $d . $y) ==  {
        if (date('Y') - $y >= 18) {
            if ($m > date('m') || $d > date('d')) {
                return $msg;
            }

            $msg = $m.'-'.$d.'-'.$y;
        }
    }

    return $msg;
}
?>
sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

Post by sansoo »

sorry it took awhile to get back to my post.

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

They are part of the alternative sntax for if else statements...

Code: Select all

$varvalue = ( $if_this_is_true ) ? do_this() : else_do_this();
sansoo
Forum Commoner
Posts: 32
Joined: Mon Aug 14, 2006 5:33 pm
Location: Smallville

Post by sansoo »

I got it. Like short hand in CSS. Cuts down on read time by limiting the number of characters and lines of code.

Now where in the !@#$ is someone supposed to learn that from. Besides on a forum like this from someone like you.

Wait...let me gues...The big ol' confusing manual called PHP.net

see im learnin...just takes time and a deisel tanker full of coffee
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For future reference, it's called the ternary or trinary operator. Usually written shorthand as "?:"
Post Reply