0

in my model

$this->db->select('message,TIMESTAMPDIFF(MINUTE,date_added,CURRENT_TIMESTAMP) AS minutes_passed');
    $this->db->from('activity_logs');
    $this->db->order_by('log_id','desc');
    $this->db->limit(20);
    $q = $this->db->get();
    return $q->result_array();

in controller

 $data['logs'] = $this->common_model->get_logs();

    foreach ($data['logs'] as $k) {
      echo 'total minutes'.$k['minutes_passed']."<br>";//output total minutes "21410"
      }

i have total number of minutes and i want to get total hours related to minutes and then hours to convert into total days. i hope anyone can guide me through this. Thanks.

Abdul Basit
  • 125
  • 1
  • 3
  • 14

3 Answers3

4

Try this function,

& Modify it with your requirements

    function con_min_days($mins)
    {

            $hours = str_pad(floor($mins /60),2,"0",STR_PAD_LEFT);
            $mins  = str_pad($mins %60,2,"0",STR_PAD_LEFT);

            if((int)$hours > 24){
            $days = str_pad(floor($hours /24),2,"0",STR_PAD_LEFT);
            $hours = str_pad($hours %24,2,"0",STR_PAD_LEFT);
            }
            if(isset($days)) { $days = $days." Day[s] ";}

            return $days.$hours." Hour[s] ".$mins." Min[s]";
    }
Hytool
  • 1,358
  • 1
  • 7
  • 22
1

select field from database like UNIX_TIMESTAMP(date_added) as create_date

add this function

private function time_elapsed_string($ptime)
{
    $etime = time() - $ptime;
    if ($etime < 1){
        return '0 seconds';
    }
    $a = array( 365 * 24 * 60 * 60  =>  'year',
                 30 * 24 * 60 * 60  =>  'month',
                      24 * 60 * 60  =>  'day',
                           60 * 60  =>  'hour',
                                60  =>  'minute',
                                 1  =>  'second'
                );
    $a_plural = array( 'year'   => 'years',
                       'month'  => 'months',
                       'day'    => 'days',
                       'hour'   => 'hours',
                       'minute' => 'minutes',
                       'second' => 'seconds'
                );
    foreach ($a as $secs => $str){
        $d = $etime / $secs;
        if ($d >= 1){
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
        }
    }
}

and use it like this :

echo $this->time_elapsed_string($item->create_date)

O/P :

2 hour ago
5 days ago

depends on time in millisecond you passed in function

Pratik Bhalodiya
  • 736
  • 7
  • 14
0

I think you should simply get the date from your database, and make the whole thing in php :

$now = new DateTime;
$date_added = new DateTime;
$date_added->setTimestamp(date_added);

$diff = $now->diff($date_added);

Then you can print :

sprintf("Temps écoulé : %d jours, %d minutes et %d seconds"; $diff->d, $diff->h, $diff->s);

PS : you should not store a timestamp in your database, but a date using DATE or DATETIME field type.

JesusTheHun
  • 1,217
  • 1
  • 10
  • 19