Welcome Guest, Not a member yet? Register   Sign In
Where does a thing like this go?
#1

[eluser]digity[/eluser]
(I apologize in advance for not using proper lingo - I'm still learning)

I've got a bunch of code I wrote 6 years ago (for an old PHP/MySQL app I never finished) and I'd like to use some of them in current PHP/MySQL apps I'm developing with CodeIgniter. I understand to NOT incorporate them in a specific controller since I'd like any controller to use/access them, but I'm not sure if they go into helpers, libraries, hooks or some other place. In addition to that I'm not sure how to actually incorporate them.

Below is an example of the code I'd like to bring over (I don't care how bad the below code is or how CI has stuff built-in that already does this - I just want to know how to incorporate my old stuff like this). It sat in a file called inc.functions.php that I called (require() or include(), I forget exactly).

Code:
function getrelevantTime($date_time) {

    /*
    60 = minute
    3600 = hour
    86400 = day
    604800 = week
    2592000 = month (day x 30)
    31536000 = year (day x 365)
    31622400 = leap year (day x 366)

    night = 18:00:00 to 03:59:59 (6 pm to 3:59 am)
    afternoon = 12:00:00 to 05:59:59 (12 noon to 5:59 pm)
    morning = 04:00:00 to 11:59:59 (4 am to 11:59 am)


    $date_timeArray = explode(" ", $tap_date_time);
    $dateArray = explode("-", $date_timeArray[0]);
    $timeArray = explode(":", $date_timeArray[1]);

    $y = date("g:i a" , strtotime("$date_timeArray[1]"));

    $zTap_date_time = $dateArray[1] . "/" . $dateArray[2] . " @ " . $y;
    */

    // get relevant time info
    // $now_date_timestamp = date("Y-m-d H:i:s");
    $now_date_timestamp = date("U");
    $tap_date_timestamp = date("U", strtotime("$date_time"));
    $difference = $now_date_timestamp - $tap_date_timestamp;


    // convert bytes (B) to gigabytes (GB), megabyates (MB) & kilobytes (KB)
    if ($difference >= 31536000) {
    // year or more
    
        // $zDifference = bcdiv($difference, 31536000, 0) . " years";
        $zDifference = $difference / 31536000;
        $zDifference = round($zDifference, 0) . " years";
    
    } else if ($difference >= 2592000) {
    // month or more

        // $zDifference = bcdiv($difference, 2592000, 0) . " months";
        $zDifference = $difference / 2592000;
        $zDifference = round($zDifference, 0) . " months";

    } else if ($difference >= 604800) {
    // week or more

        // $zDifference = bcdiv($difference, 604800, 0) . " weeks";
        $zDifference = $difference / 604800;
        $zDifference = round($zDifference, 0) . " weeks";

    } else if ($difference >= 86400) {
    // day or more

        // $zDifference = bcdiv($difference, 86400, 0) . " days";
        $zDifference = $difference / 86400;
        $zDifference = round($zDifference, 0) . " days";

    } else if ($difference >= 3600) {
    // hour or more

        // $zDifference = bcdiv($difference, 3600, 0) . " hours";
        $zDifference = $difference / 3600;
        $zDifference = round($zDifference, 0) . " hours";

        $now_date = date("d");
        $tap_date = date("d", strtotime("$date_time"));
        $now_hour = date("H");
        $tap_hour = date("H", strtotime("$date_time"));


        if ($tap_date < $now_date)  {
        // tapped yesterday
            
        }

    } else if ($difference >= 60) {
    // minute or more

        // $zDifference = bcdiv($difference, 60, 0) . " minutes";
        $zDifference = $difference / 60;
        $zDifference = round($zDifference, 0) . " minutes";

    } else {
    // seconds or more

        $zDifference = $difference . " seconds";
        
    }

    return $zDifference;

}


Thanks in advance!
#2

[eluser]InsiteFX[/eluser]
I would crate a helper for it.

CodeIgniter User Guide - Helper Functions

InsiteFX
#3

[eluser]digity[/eluser]
Thanks! Just curious: at what point do I create a new library over a new helper?
#4

[eluser]InsiteFX[/eluser]
When you have a lot of functions/methods that relate to one another then a Library would be useful.

InsiteFX
#5

[eluser]digity[/eluser]
Thanks! I got it working, much easier than I thought. Thanks again!
#6

[eluser]InsiteFX[/eluser]
Your very Welcome!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB