Welcome Guest, Not a member yet? Register   Sign In
Where to put a function to calculate age...?
#1

[eluser]Sinclair[/eluser]
Hi,

My question is about design in CI.

I have my Model with functions that do SELECTS to the database, but I need also to use functions like this:

Code:
#
function GetAge($Birthdate)
#
{
#
        // Explode the date into meaningful variables
#
        list($BirthYear,$BirthMonth,$BirthDay) = explode("-", $Birthdate);
#
        // Find the differences
#
        $YearDiff = date("Y") - $BirthYear;
#
        $MonthDiff = date("m") - $BirthMonth;
#
        $DayDiff = date("d") - $BirthDay;
#
        // If the birthday has not occured this year
#
        if ($DayDiff < 0 || $MonthDiff < 0)
#
          $YearDiff--;
#
        return $YearDiff;
#
}
#
?&gt;

Where can I put functions like this in CI?


Best Regards,
#2

[eluser]jedd[/eluser]
This is crying out to take up residence in a [url="/user_guide/general/helpers.html"]helper[/url].
#3

[eluser]Sinclair[/eluser]
Thanks. I'am writting a helper but I have some strange errors:

The function is this one here:

Code:
&lt;?PHP
// $BirthDate expected format is YYYY/MM/DD
function CalculateAge($BirthDate)
{
        // Put the year, month and day in separate variables
        list($Year, $Month, $Day) = explode("/", $BirthDate);

        $YearDiff = date("Y") - $Year;

        // If the birthday hasn't arrived yet this year, the person is one year younger
        if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff))
        {
                $YearDiff--;
        }
        return $YearDiff;
}
?&gt;


I have several errors like this:

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 2

Filename: helpers/my_calculateage_helper.php

Line Number: 6

Quote:A PHP Error was encountered
Severity: Warning

Message: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '0.0/no DST' instead

Filename: helpers/my_calculateage_helper.php

Line Number: 8

The HELPER is working but display these errors... what can I do to solve this?

Best Regards,
#4

[eluser]CI Coder[/eluser]
Well, it depends when you need it and how broad a scope you want. If you need it in all controllers and models, etc., put it in a helper file; if you only need it in that model leave it only there. The latter option makes portability a bit easier in the sense that you don't have to remember to move the helper file with the model all the time, but that's minor.
#5

[eluser]Sinclair[/eluser]
I think the best solution is to use a HELPER. But how to fix the errors?

Best Regards.
#6

[eluser]CI Coder[/eluser]
[quote author="Sinclair" date="1258947899"]Thanks. I'am writting a helper but I have some strange errors:

The function is this one here:

Code:
&lt;?PHP
// $BirthDate expected format is YYYY/MM/DD
function CalculateAge($BirthDate)
{
        // Put the year, month and day in separate variables
        list($Year, $Month, $Day) = explode("/", $BirthDate);

        $YearDiff = date("Y") - $Year;

        // If the birthday hasn't arrived yet this year, the person is one year younger
        if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff))
        {
                $YearDiff--;
        }
        return $YearDiff;
}
?&gt;


I have several errors like this:

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 2

Filename: helpers/my_calculateage_helper.php

Line Number: 6

Quote:A PHP Error was encountered
Severity: Warning

Message: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '0.0/no DST' instead

Filename: helpers/my_calculateage_helper.php

Line Number: 8

The HELPER is working but display these errors... what can I do to solve this?

Best Regards,[/quote]

Go into your php.ini and set the default time zone for your PHP installation. Add a line like "date.timezone = GMT".
#7

[eluser]jedd[/eluser]
The date change happened in 5.3.x from memory - confused a few people. Previously PHP had made a few assumptions on our behalf, but now they're not being so assumptive ... pluses and minuses.

The other error you're seeing - did you check your incoming date has two /'s in it? I'd be putting an echo in there just to check what you've got coming in. Alternatively dump it into a straight array (not using list()) and then access the fields directly.
#8

[eluser]Sinclair[/eluser]
The first error was solved in the php.ini.

The second error was solved changing this line from:

This
Code:
list($Year, $Month, $Day) = explode("/", $BirthDate);

To

This
Code:
list($Year, $Month, $Day) = explode("-", $BirthDate);

Thanks a lot for your help.

Best Regards.
#9

[eluser]CI Coder[/eluser]
Yes, MySQL date is always stored as YYYY-MM-DD. Glad it worked out for you.




Theme © iAndrew 2016 - Forum software by © MyBB