Skip to content

Instantly share code, notes, and snippets.

@devyfriend
Last active December 11, 2015 23:58
Show Gist options
  • Save devyfriend/4680126 to your computer and use it in GitHub Desktop.
Save devyfriend/4680126 to your computer and use it in GitHub Desktop.
my Codeigniter common helper
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
function test($x,$exit=0)
{
echo $res = "<pre>";
if(is_array($x) || is_object($x)){
echo print_r($x);
}else{
echo var_dump($x);
}
echo "</pre><hr />";
if($exit==1){ die(); }
}
function clean_string($str)
{
$str = str_replace('\"','"', $str);
$str = str_replace("\'","'", $str);
return $str;
}
function asset_url($path = '')
{
$ci =& get_instance();
return config_item('asset_url').$path;
}
function twitter_time($time)
{
$t = date_parse($time);
$t = mktime($t['hour'],$t['minute'],$t['second'],$t['day'],$t['month'],$t['year']);
$t = date('j M Y, H:i',$t);
return $t;
}
function twitter_time_to_db($time)
{
$t = date_parse($time);
$t = mktime($t['hour'],$t['minute'],$t['second'],$t['day'],$t['month'],$t['year']);
$t = date('Y-m-d, H:i:s',$t);
return $t;
}
function twitter_age($time)
{
$t = date_parse($time);
$a = new DateTime($t['year'].'-'.$t['month'].'-'.$t['day']);
test(date_diff($a, '2011-01-01')); exit;
$t = mktime($t['hour'],$t['minute'],$t['second'],$t['day'],$t['month'],$t['year']);
$t = date_diff(time() - $t);
return $t;
}
function jsout($output)
{
header('Content-type: application/json');
exit( json_encode( $output ) );
}
function goingto($url='')
{
if($url == '') $url = BASE_URL;
header('location: '.$url);
exit;
}
function send_email($to, $from, $subject, $message)
{
$ci =& get_instance();
$ci->load->library('Email');
$email = $ci->email;
$email->from($from);
$email->to($to);
$email->subject($subject);
$email->message($message);
return $email->send();
}
function do_session($act,$key,$data = array())
{
$ci =& get_instance();
switch($act)
{
case 'get':
$tmp = $ci->session->userdata($key);
$ci->session->unset_userdata($key);
return $tmp;
break;
case 'set':
$ci->session->set_userdata($key,$data);
break;
default: exit('Invalid do session');
}
}
function load_errors()
{
$ci =& get_instance();
$err = do_session('get','errors');
$ci->load->helper('html');
$err = '<div class="errors_found">'.ul($err).'</div>';
if(IS_AJAX)
{
jsout(false, array('errors' => $err));
}else{
return $err;
}
}
function enkript($val)
{
$ci =& get_instance();
return $ci->encrypt->encode($val);
}
function dekript($val)
{
$ci =& get_instance();
return $ci->encrypt->decode($val);
}
function phpdbnow()
{
$now = getdate();
return $now['year'].'-'.$now['mon'].'-'.$now['mday'].' '.$now['hours'].':'.$now['minutes'].':'.$now['seconds'];
}
function ordinal(num) {
return num + (
(num % 10 == 1 && num % 100 != 11) ? 'st' :
(num % 10 == 2 && num % 100 != 12) ? 'nd' :
(num % 10 == 3 && num % 100 != 13) ? 'rd' : 'th'
);
}
function setKeyVal($data,$value)
{
$tmp = false;
if($data && count($data))
{
foreach($data as $key=>$val){
$tmp[$val['id']] = $val[$value];
}
}
return $tmp;
}
function randomstring($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'tahun',
'm' => 'bulan',
'w' => 'minggu',
'd' => 'hari',
'h' => 'jam',
'i' => 'menit',
's' => 'detik',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
// $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
$v = $diff->$k . ' ' . $v;
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' yang lalu' : 'baru saja';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment