Welcome Guest, Not a member yet? Register   Sign In
Thought I'd post some of my helpers for others
#1

[eluser]nuwanda[/eluser]
message_redirect combines the session flashdata and redirect in one go to make things a little easier in controllers.

time_ago is like the CI function but a little more customised. The $date is a datetime field from mysql.

print_object I use for debugging. It simply formats array or object output.

array_trim trims spaces from all array elements.

array_trim_str trims the given character from array elements.

Hope these help others.

Code:
<?php

  function message_redirect($path='/', $message=''){
  
    get_instance()->session->set_flashdata('message', $message);
    redirect($path, 'location');
  
  }

  
  function time_ago($date){

      $timestamp = strtotime($date);

    $diff = time() - $timestamp;
    
    if($diff < 60)
      return $diff . ' second' . ((floor($diff) == 1)?'':'s') . ' ago';
    if($diff < 3600)
      return floor($diff / 60) . ' minute' . ((floor($diff / 60) == 1)?'':'s') . ' ago';
    if($diff < 86400)
      return floor($diff / 3600) . ' hour' . ((floor($diff / 3600) == 1)?'':'s') . ' ago';
    if($diff < 172800)
      return 'yesterday';
    if($diff < 259200)
      return '2 days ago';
    else
      //for years other than current year, show the year
      $timestamp_year = date('Y', $timestamp);
      $year = (date('Y') != $timestamp_year) ? ', ' . $timestamp_year : '';
      return date('M j', $timestamp) . $year . date(' \a\t G:i', $timestamp);
    
  }
  
        
  function print_object($obj){
  
    echo '<pre>';
    print_r($obj);
    echo '</pre>';
  
  }

  
  function array_trim($array){
  
    return array_map('trim', $array);
  
  }

  
  function array_trim_str($array, $trim_str = ''){
  
      if($trim_str == '') return $array;

    $regex = '/^' . $trim_str . '+|' . $trim_str . '+$/';
  
    return preg_replace($regex, '', $array);
  
  }
  
//eof




Theme © iAndrew 2016 - Forum software by © MyBB