Question: I am trying to make some helper in view part ,so that I can share the common logic in the view part of whole project .

But it seems in CI ,there is no such concept ,like in CakePhp ,i can use the custom helper .

I have searched a lot but not getting any proper results to carry out .

Below I have mentioned some codes of my view parts.

```html
<?php  $dt2 = strtotime($single->time_stamp);
 $dt1 = strtotime(date("Y-m-d H:i:s"));
  echo (int)(abs($dt1 - $dt2)/(60*60));?>

```

I have used this codes in view ,but I want to reuse it .

I want to call a custom helper passing one argument ,as another one is the current date.

Any suggestion?

Thank you in advance .

Answer: 
This would be your helper application/helpers/time\_diff\_helper.php: 

```
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

if (!function_exists('time_diff'))
{
    function time_diff()
    {
        $dt2 = strtotime($single->time_stamp);
        $dt1 = strtotime(date("Y-m-d H:i:s"));
        return (int)(abs($dt1 - $dt2)/(60*60));
    }
}

```

You would then load it in your controller: 

```
$this->load->helper('time_diff');
$tdiff = time_diff();

```

If you load it into your controller, you should also be able to use it from within your views. People would argue that business logic doesn't belong in views, that view are juts there to format the display. Is your function part of the business logic or just part of the layout... ? I guess it's a matter of interpretation. I would call it in the controller and pass it to the view. 

```
$data['time_diff'] = time_diff();
$this->load->view('myscreen1', $data);

```
You can create custom helpers using CodeIgniter. You have to put them into application/helpers. 

Here is how: 

```
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

if (!function_exists('resize_uploaded_pix'))
{
    function resize_uploaded_pix($pic_full_path, $formatname)
    {
        //global $image_format;

        $config['image_library'] = 'gd2';
        $config['source_image']     = $pic_full_path; 
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']   = TRUE;

        /*
         * get session load lib and config
         */
        $CI =& get_instance();
        $CI->load->library('image_lib');
        $CI->config->load('myconfig', TRUE);
        $image_format = $CI->config->item('image_format', 'myconfig');   

```

You can't access $this from within a helper. So if you need to use other libs in it, you need to get the instance pointer like this:

```
$CI =& get_instance();
$CI->load->library('image_lib');

```

Then call the helper code from within your code, in this case a controller (but should work the same from within a view), like this:

```
$this->load->helper('resize_pix');
resize_uploaded_pix($upload_data['upload_data']['full_path'], 'default');

```