Welcome Guest, Not a member yet? Register   Sign In
How to use timespan helper in CodeIgniter 4 ?
#1

Hello,
How to use timespan helper in CodeIgniter 4 ?
I need ago time in my CI4 project. I used timespan($post_date, $now, $units); on CI3 but I can't use it on CI4.
Reply
#2

(This post was last modified: 09-22-2020, 02:39 PM by InsiteFX.)

This is not tested but should get you going I have converted everything to CodeIgniter 4.
I believe the timeSpan helper is from Lonnie and his BonFire.

app/Language/en/Date.php

PHP Code:
<?php

namespace App\Language\en\Date;

return [
    
'dateYear'    => 'Year',
    
'dateYears'   => 'Years',
    
'dateMonth'   => 'Month',
    
'dateMonths'  => 'Months',
    
'dateWeek'    => 'Week',
    
'dateWeeks'   => 'Weeks',
    
'dateDay'     => 'Day',
    
'dateDays'    => 'Days',
    
'dateHour'    => 'Hour',
    
'dateHours'   => 'Hours',
    
'dateMinute'  => 'Minute',
    
'dateMinutes' => 'Minutes',
    
'dateSecond'  => 'Second',
    
'dateSeconds' => 'Seconds',

    
'UM12'   => '(UTC -12:00) Baker/Howland Island',
    
'UM11'   => '(UTC -11:00) Niue',
    
'UM10'   => '(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti',
    
'UM95'   => '(UTC -9:30) Marquesas Islands',
    
'UM9'    => '(UTC -9:00) Alaska Standard Time, Gambier Islands',
    
'UM8'    => '(UTC -8:00) Pacific Standard Time, Clipperton Island',
    
'UM7'    => '(UTC -7:00) Mountain Standard Time',
    
'UM6'    => '(UTC -6:00) Central Standard Time',
    
'UM5'    => '(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time',
    
'UM45'   => '(UTC -4:30) Venezuelan Standard Time',
    
'UM4'    => '(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time',
    
'UM35'   => '(UTC -3:30) Newfoundland Standard Time',
    
'UM3'    => '(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay',
    
'UM2'    => '(UTC -2:00) South Georgia/South Sandwich Islands',
    
'UM1'    => '(UTC -1:00) Azores, Cape Verde Islands',
    
'UTC'    => '(UTC) Greenwich Mean Time, Western European Time',
    
'UP1'    => '(UTC +1:00) Central European Time, West Africa Time',
    
'UP2'    => '(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time',
    
'UP3'    => '(UTC +3:00) Moscow Time, East Africa Time, Arabia Standard Time',
    
'UP35'   => '(UTC +3:30) Iran Standard Time',
    
'UP4'    => '(UTC +4:00) Azerbaijan Standard ime, Samara Time',
    
'UP45'   => '(UTC +4:30) Afghanistan',
    
'UP5'    => '(UTC +5:00) Pakistan Sta,ard Time, Yekaterinburg Time',
    
'UP55'   => '(UTC +5:30) Indian Standard Time, Sri Lanka Time',
    
'UP575'  => '(UTC +5:45) Nepal Time',
    
'UP6'    => '(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time',
    
'UP65'   => '(UTC +6:30) Cocos Islands, Myanmar',
    
'UP7'    => '(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam',
    
'UP8'    => '(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time',
    
'UP875'  => '(UTC +8:45) Australian Central Western Standard Time',
    
'UP9'    => '(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time',
    
'UP95'   => '(UTC +9:30) Australian Central Standard Time',
    
'UP10'   => '(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time',
    
'UP105'  => '(UTC +10:30) Lord Howe Island',
    
'UP11'   => '(UTC +11:00) Srednekolymsk Time, Solomon Islands, Vanuatu',
    
'UP115'  => '(UTC +11:30) Norfolk Island',
    
'UP12'   => '(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time',
    
'UP1275' => '(UTC +12:45) Chatham Islands Standard Time',
    
'UP13'   => '(UTC +13:00) Samoa Time Zone, Phoenix Islands Time, Tonga',
    
'UP14'   => '(UTC +14:00) Line Islands',
]; 

app/Helpers/time_helper.php

PHP Code:
<?php

namespace App\Helpers;

/**
 * -----------------------------------------------------------------------
 *  timeSpan ()
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('timeSpan'))
{
    function 
timeSpan($seconds 1$time ''$units 7)
    {
        
is_numeric($seconds) or $seconds 1;
        
is_numeric($time)    or $time    time();
        
is_numeric($units)   or $units   7;

        
$seconds = ($time <= $seconds) ? $time $seconds;

        
$str   = [];

        
$years floor($seconds 31557600);

        if (
$years 0)
        {
            
$str[] = $years ' ' lang($years 'Date.dateYears' 'Date.dateYear');
        }

        
$seconds -= $years 31557600;

        
$months floor($seconds 2629743);

        if (
count($str) < $units && ($years or $months 0))
        {
            if (
$months 0)
            {
                
$str[] = $months ' ' lang($months 'Date.dateMonths' 'Date.dateMonth');
            }

            
$seconds -= $months 2629743;
        }

        
$weeks floor($seconds 604800);

        if (
count($str) < $units && ($years or $months or $weeks 0))
        {
            if (
$weeks 0)
            {
                
$str[] = $weeks ' ' lang($weeks 'Date.dateWeeks' 'Date.dateWeek');
            }

            
$seconds -= $weeks 604800;
        }

        
$days floor($seconds 86400);

        if (
count($str) < $units && ($months or $weeks or $days 0))
        {
            if (
$days 0)
            {
                
$str[] = $days ' ' lang($days 'Date.dateDays' 'Date.dateDay');
            }

            
$seconds -= $days 86400;
        }

        
$hours floor($seconds 3600);

        if (
count($str) < $units && ($days or $hours 0))
        {
            if (
$hours 0)
            {
                
$str[] = $hours ' ' lang($hours 'Date.dateHours' 'Date.dateHour');
            }

            
$seconds -= $hours 3600;
        }

        
$minutes floor($seconds 60);

        if (
count($str) < $units && ($days or $hours or $minutes 0))
        {
            if (
$minutes 0)
            {
                
$str[] = $minutes ' ' lang($minutes 'Date.dateMinutes' 'Date.dateMinute');
            }
            
$seconds -= $minutes 60;
        }

        if (
count($str) === 0)
        {
            
$str[] = $seconds ' ' lang($seconds 'Date.dateSeconds' 'Date.dateSecond');
        }

        return 
implode(', '$str);
    }
}

/**
 * -----------------------------------------------------------------------
 * Filename: time_helper.php
 * Location: ./app/Helpers/time_helper.php
 * -----------------------------------------------------------------------
 */ 

Just use helper('time'); in your controller and you should be good to go.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks a lot.
It worked. But I removed namespace too.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB