almightyegg Posted April 5, 2007 Share Posted April 5, 2007 I want to store a date of when smebody created their account and from that calculate days/years old... How would I do this? I don't think it work with a standard timestamp but I'm not sure ??? Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 http://nl2.php.net/date $year = date("Y"); $yearsold = $year - registerdyear; etc, very simple... Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 Sorry I didn't make myself very clear... I want to show like: Age: 4 years, 278 days Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 hm, youve now got me cofused :-\, ill get back to you when i figure it Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 figured it.. $year = date("y"); $yearsold = $year - registerdyear; $day = date("z"); $daysold = dayregisterd - $day; if(daysold<0){ $year = date("y") - 1; $daysold = dayregisterd - $day + 360; } hope it works, ive never tried this before Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 Age: -1995 years, 18 days hmmm...I shouldn't be born for another 1995 years :-X $year = date("y"); $yearsold = $year - $mem[year]; $day = date("z"); $daysold = $mem[day] - $day; if($daysold<0){ $year = date("y") - 1; $daysold = $mem[day] - $day + 360; } I put in my row: year - 2002 day - 112 So the 18 is right I think...the year definitely isn't Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 lol it should be a capital Y soz $year = date("Y"); Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 5 years 18 days.... I just counted up and it should be 4 years 347 days... Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 $year = date("Y"); $yearsold = $year - $mem[year]; $day = date("z"); $daysold = $mem[day] - $day; if($daysold<0){ $year = date("Y") - 1; $daysold = $mem[day] - $day + 365; } lmao i dont even know how many days there are in a year Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 No change in the outcome... Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 im gonna try it out myself, il get back to you when i get it working Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 I have it working now $year = date("Y"); $yearsold = $year - $mem[year]; $day = date("z"); $days = $mem[day] - $day; $daysold = 365 - $days; if($daysold > $days){ $year = date("Y") - 1; $yearsold = $year - $mem[year]; } Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 ooo never thought of that, thx btw, i need it too Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 doesnt work :-\ look try this <? $memyear = 2002; $memday = 93; $year = date("Y"); $yearsold = $year - $memyear; $day = date("z"); $days = $memday - $day; $daysold = 365 - $days; if($daysold > $days){ $year = date("Y") - 1; $yearsold = $year - $memyear; } echo "$daysold: $yearsold"; ?> today is the 95th day, its returning that ive been registerd for 367 days and 4 years, should be 5 years and 2 days... Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 that is 5 years and 2 days because there is more than a yearsworth of days... try adding this (before you echo it out ): elseif($daysold >= 365){ $year2 = $year + 1; $yearsold = $year2 - $memyear; } Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 $year = date("Y"); $yearsold = $year - $player[year]; $day = date("z"); $days = $player[day] - $day; $daysold = 365 - $days; if($daysold > $day){ $year = date("Y") - 1; $yearsold = $year - $player[year]; }elseif($daysold > 365){ $year = date("Y") + 1; $yearsold = $year - $player[year]; } This version works I believe Quote Link to comment Share on other sites More sharing options...
only one Posted April 6, 2007 Share Posted April 6, 2007 lol nope Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted April 6, 2007 Share Posted April 6, 2007 Wait, if you can create a timestamp of their date of birth, you can get the number of seconds they have been born ("now" timestamp - birthdate timestamp), divide that by 86400 (the number of seconds in a day,) to get days. Divide the days by 365 and you have how many years, then mod the days by 365 to get the remainder, and you have years and days. Simple. Basically: <?php $day = [database query to get day of birth]; $month = [database query to get month of birth IN TEXT FORMAT (ex: "august")]; $year = [database query to get year of birth]; $dob = strtotime($day . $month . $year); $now = strtotime("now"); $sec_diff = $now - $dob; $days = $sec_diff / 86400; $years = floor($days / 365); $days = $days % 365; echo "You were born " . $years . " years, " . $days . " days ago."; ?> I hope this works. You might need to do some tweaking, but basically the $day, $month, and $year variables are what the user put into the text fields for date of birth, for day of the month, month, and year. You also might need to do some calculations to determine whether it is a leap year, but I'm not entirely sure. edit: I tested this, and it worked, the values I used for $day, $month, and $year were 17, "august" and 1989 respectively (my birthday) and it returned "You were born 17 years, 236 days ago." The reason for the floor() function in there is becuase without it, you get a crazy long decimal for the year, floor rounds down, no matter how large the decimal is, meaning that it could be 17.99999 and it would round down to 17, which in this case is what we need, since you are also getting the hours with the mod algorithm. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted April 7, 2007 Share Posted April 7, 2007 Does this help you? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.