Wednesday, 15 February 2017

Calculate day hours and minutes between Registration Date and current Datetime.

Make this function into your Model.

function dateDiff($date)
    {
      $mydate= date("Y-m-d H:i:s");
      $theDiff="";
      //echo $mydate;//2014-06-06 21:35:55
      $datetime1 = date_create($date);
      $datetime2 = date_create($mydate);
      $interval = date_diff($datetime1, $datetime2);
      //echo $interval->format('%s Seconds %i Minutes %h Hours %d days %m Months %y Year    Ago')."<br>";
      $min=$interval->format('%i');
      $sec=$interval->format('%s');
      $hour=$interval->format('%h');
      $mon=$interval->format('%m');
      $day=$interval->format('%d');
      $year=$interval->format('%y');
      if($interval->format('%i%h%d%m%y')=="00000")
      {
        //echo $interval->format('%i%h%d%m%y')."<br>";
        return $sec." Seconds";

      }

    else if($interval->format('%h%d%m%y')=="0000"){
       return $min." Minutes";
       }

    else if($interval->format('%d%m%y')=="000"){
       return $hour." Hours";
       }

    else if($interval->format('%m%y')=="00"){
       return $day." Days";
       }

    else if($interval->format('%y')=="0"){
       return $mon." Months";
       }

    else{
       return $year." Years";
       }

    }



Now call this model into your function in controller like this..

public function home()
    {
        $this->load->model('mode');
        $data['main_content'] = 'home';
        $this->load->view('templates',$data);
   }


Now write this code into your view

<?php echo date('Y-m-d', strtotime($row->edate)) ;?>&nbsp;&nbsp;&nbsp;<i class='fa fa-clock-o'></i>&nbsp;<?php echo $this->mode->dateDiff($row->edate) ;?>

// edate comes from database.



0 comments:

Post a Comment