RoninStretch Posted August 11, 2007 Share Posted August 11, 2007 Ok, fairly noob at php so please bear with me.. I'm doin the old calculate persons age from database routine. Had a good search around and found out about Unix timestamps. Been looking those up and trying to figure out how to use them properly. My dates are of the d-m-Y format.. I.E.. today is 11-02-2007 I did it this way so i can use date("d-m-Y") to grab todays date elsewhere in the site and it's all the same format. Problem is that I can't quite grasp timestamps. I tired something like this.. $dobts = strtotime($dateofbirth); $todaysdate = date("d-m-Y"); $todaysdatets = strtotime($todaysdate); $agets = $todaysdatets - $dobts; $age = date("d-m-Y", $agets); Didn't work of course but it thought that was the point of these timestamps? Can anyone explain it better please? Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/ Share on other sites More sharing options...
RoninStretch Posted August 11, 2007 Author Share Posted August 11, 2007 ok tried a bit more.. Had another look round and found a post or two that explained a little better.. but it's still not working. here's the code now.. $todaysdate = date("d-m-Y"); $diff = abs(strtotime($todaysdate) - strtotime($dateofbirth)); $day = 60 * 60 * 24; // seconds in a day $agedays = $diff / $day; $age = floor($agedays/365); but with $todaysdate = 22-05-1982 it chucks out 10... and should be 25. I think maybe the strtotime doesn't like my date inputs? Having a little read of the immense php manual tells me it takes a US date input.. so is that the problem? if so it means re-writing alot of code or figuring a way to trick strtotime().. So if this is the problem does anyone know a way of getting 22-05-1982 to 05-22-1982 before i use strtotime() on it? any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-321078 Share on other sites More sharing options...
Barand Posted August 11, 2007 Share Posted August 11, 2007 Don't store dates as d-m-y , it's useless as a date format when storing in a db. Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-321115 Share on other sites More sharing options...
RoninStretch Posted August 11, 2007 Author Share Posted August 11, 2007 ok well thanks for that... but what should i be doing? Any help or links to reference, search terms... anything would help thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-321122 Share on other sites More sharing options...
lightningstrike Posted August 11, 2007 Share Posted August 11, 2007 Save the data as a timestamp usually //get timestamp in seconds and store in database $timestamp = time(); //load timestamp from database then //code to print date according to timestamp's value $date = date("M-d-Y",$timestamp); Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-321127 Share on other sites More sharing options...
RoninStretch Posted August 11, 2007 Author Share Posted August 11, 2007 Ok i will save dates as a timstamp then.. makes sense. thanks guys will have to try that later tho. Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-321147 Share on other sites More sharing options...
Barand Posted August 11, 2007 Share Posted August 11, 2007 I prefer DATE or DATETIME types yyyy-mm-dd or yyyy-mm-dd hh:nn:ss Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-321289 Share on other sites More sharing options...
RoninStretch Posted August 15, 2007 Author Share Posted August 15, 2007 Still having trouble with this.. Lots of trouble.. ??? Anyways.. so i have three list boxes for users to enter their date of birth. Days, months and years. I can change this to a timestamp now using strtotime. I think i'm doing it right.. $dateofbirth = $dobyear . "-" . $dobmonth . "-" . $dobday; $dobts = strtotime($dateofbirth); now i store $dobts in my database.. Now the problem comes trying to display the persons age. I grab the current timestamp- $currentts = time(); then I'm guessing you subtract the date of birth from the current time.. leaving us with the amount of seconds since this person was born. $agesecs = $currentts - $dobts; then some trickery to make it into years... which im also kinda guessing at.. $age = $agesecs *60 *60 *24 *365; but with the date of birth 22nd May 1982 this gives me the amazing out put of - 2.5112491353072E+16 so yeah... wrong.. please help, I don't have much hair left Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-324586 Share on other sites More sharing options...
Barand Posted August 15, 2007 Share Posted August 15, 2007 You need to divide by seconds in a year, not multiply. try <?php $dobts = mktime(0,0,0,5,22,1982); $age = date('Y') - date('Y', $dobts); if (date('md') < date('md', $dobts)) $age--; echo $age; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64388-date-calculations-clear-things-up-for-me-please/#findComment-324679 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.