-3

I am facing a problem while calculating age saved in the database. I obtained help from a user of this site and he gave me the proper output file but i ran into anoth issue. The solution is made for newer php version but the php version i have is 5.2.6. Any help in this method will be highly appreciated. Here is the code he provided me

if(!empty($row11['dob'])){ 
            $birthdate = new DateTime($row11['dob']);
            $today     = new DateTime('today');
            $ag = $birthdate->diff($today)->y;
            $mn = $birthdate->diff($today)->m;
            $dy = $birthdate->diff($today)->d;              
        }else{
            $ag = 0; $mn=0; $dy=0;
        }   

I am also attaching the original file so that you can help me better. Please do note that i am using php 5.2.6 for this purpose. Any prompt help in this regard will be highly appreciated. Thanks

Here is the link to Original File

  • Are you not able to _update_ your PHP? That version is 9 years old! – GrumpyCrouton Oct 24 '17 at 14:53
  • We are running a charity school and someone donated us the examinatino system made in php. In that system we are printing results of students and we also require the age to be printed on the result card in YY-MM-DD format. we do not have access to the developer that is why i am here and in hope that the pro's or this community will surely help. – Robert M. Smith Oct 24 '17 at 14:55
  • It would help us if we knew the format that everything was in. What format is the `dob` cell in the database? What is your expected result, and your current result? – GrumpyCrouton Oct 24 '17 at 14:57
  • If you see the original file attached you can find all the details regarding the details also. the dob is saved in DD-MM-YYYY format in the database. here is an output of our result card http://spsc.1free-host.com/3%20d/1860.pdf – Robert M. Smith Oct 24 '17 at 15:01
  • So you are just trying to convert `DD-MM-YYYY` to `YY-MM-DD`? – GrumpyCrouton Oct 24 '17 at 15:02
  • no. I want to display the student age like this format age: 8 years, 3 months, 21 days from the dob saved in the database. – Robert M. Smith Oct 24 '17 at 15:03
  • 2
    Possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – GrumpyCrouton Oct 24 '17 at 15:05
  • I am a novice in this field. is it possible if you to download the original file and ammend the code for me so that i put it in the www folder and run it. ? – Robert M. Smith Oct 24 '17 at 15:08
  • Maybe you should contact a developer to do developer work for you, as I'm not going to for free. I'll help out, no problem, but I'm not going to develop and implement something for you. – GrumpyCrouton Oct 24 '17 at 15:09
  • all i asked is to ammend the code if possible. If i could hire a developer then why should i be on this forum? we are ruuning a charity school as i said earlier and are not in position to hire a developer. Anyway thanks for your help. I hope that someone will answer and solve this issue. – Robert M. Smith Oct 24 '17 at 15:12
  • @RobertM.Smith Sorry to disappoint you, but whether you are a charity organization or a Fortune 500 company is irrelevant. StackOverflow is a knowledge-sharing site for developers, not a free coding service. – Narf Oct 24 '17 at 15:25
  • Happy to say that one of this knowledge sharing site member has offered to code for me and currently working remotely on my pc. It takes nothing to help, if one want to. Thanks for sparing your time to disappoint. – Robert M. Smith Oct 24 '17 at 15:28

1 Answers1

0

Maybe I can help. Datetime->diff was introduced in PHP 5.3, so you'd need a workaround.

Not the neatest, but definitely a quick way of doing it is to substract the current date with the birthdate divided by the number of years that passed. This would look something like:

$birthdate = new DateTime($row11['dob']);
$today = new DateTime('today');
$year = round(($today->format('U') - $birthdate->format('U')) / (60*60*24*365));

For months, the situation becomes more complicated, but based on the example it doesn't seem like you need much more detail than this. If you want to account for leapyears I'd suggest extending the formula.

Friso van Dijk
  • 669
  • 3
  • 15
  • Thanks for the answer. one of the community member is also helping. in the mean time i want to share that actually the old code is printing all the details we need but the months are not printing exactly. Some students are having months in negative values... here is the old code $ag = date('Y') - substr($row11['dob'],6); $mn = date('m') - substr($row11['dob'],3,2); $dy = date('d') - substr($row11['dob'],0,2); is there any error in this one ? – Robert M. Smith Oct 24 '17 at 15:43
  • Yes. If your student was born in October and you check in May, you give it a value of `$mn = 5 - 10`, instead of adding a year to the age and having the months remaining to their birthday. – Friso van Dijk Oct 24 '17 at 20:09