Welcome Guest, Not a member yet? Register   Sign In
getAge() with wrong return
#1

Hello!

I have to check the age in my app and I use getAge() and get a wrong age. I'm in Germany, so I use German dates here:

PHP Code:
$bt2 Time::parse($this->request->getVar('geburtstag'));
echo 
$bt2->getAge() 

The dates in "geburtstag" are 05.02.2008 and 03.02.2008. Today is the 04.02.2008. But both datas returns the same age 14.

What do I not see here?
Reply
#2

See https://github.com/codeigniter4/CodeIgni...#L461-L468

It seems it considers only year.
Reply
#3

> Perfect for checking the age of someone based on their birthday:
https://codeigniter4.github.io/userguide...tml#getage

How do you calculate the age of someone?
Reply
#4

@kenjis,

If he is getting the users age should he not be using a date type method?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(02-04-2022, 04:38 PM)kenjis Wrote: See https://github.com/codeigniter4/CodeIgni...#L461-L468

It seems it considers only year.

And that's maybe wrong, because it's not the age, it's just the year.

(02-05-2022, 02:54 AM)InsiteFX Wrote: @kenjis,

If he is getting the users age should he not be using a date type method?

For me it is a little bit wired, if I have to work with time and date. So much different ways, some provided by PHP, some by CI.

Now I upgrade an other helper, which I use to calculate date-time-diffs and it works correctly. It's actually very easy ;-) Do now wonder, it was designed to use this as my favorite diff-helper and it should to more as this helper do right now.


PHP Code:
function dateTimeDiff($older$newer$return 'SEC')
{
    $date = new DateTime($older);
    $now  = new DateTime($newer);

    $diff 0;

    switch ($return)
    {
        case ('SEC'):
        {
            $hourMulti 60;
            $dayMulti  1440;

            // Minutes
            $diff $diff $date->diff($now)->s;

            // Hours
            $diff $diff $date->diff($now)->$hourMulti;

            // Days
            $diff $diff $date->diff($now)->days $dayMulti;
            
            
break;
        }
        
        
case ('YEAR'):
        {
            $diff $date->diff($now);
            
            
break;
        }
    }

    return $diff;

Reply




Theme © iAndrew 2016 - Forum software by © MyBB