Advertisement
joris

Menghitung selisi waktu

Feb 26th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. <?php
  2.  
  3. class TestController extends Controller
  4. {
  5.     public function actionIndex()
  6.     {
  7.         $time1 = '11:30';
  8.         $time2 = '12:00';
  9.         list($hours, $minutes) = explode(':', $time1);
  10.         $startTimestamp = mktime($hours, $minutes);
  11.        
  12.         list($hours, $minutes) = explode(':', $time2);
  13.         $endTimestamp = mktime($hours, $minutes);
  14.        
  15.         $seconds = $endTimestamp - $startTimestamp;
  16.         $minutes = ($seconds / 60) % 60;
  17.         $hours = round($seconds / (60 * 60));
  18.        
  19.         echo "Time passed: <b>$hours</b> hours and <b>$minutes</b> minutes";
  20.     }
  21.    
  22.     public function actionReal()
  23.     {
  24.         if(isset($_POST['Apaaja']))
  25.         {
  26.             //Collect semua data dari form
  27.             $model->attributes = $_POST['Apaaja'];
  28.            
  29.             //Ambil data waktu_awal dan waktu_akhir
  30.             $time1 = $model->waktu_awal;
  31.             $time2 = $model->waktu_akhir;
  32.            
  33.             //Prosesnya disini . . . . . .
  34.             list($hours, $minutes) = explode(':', $time1);
  35.             $startTimestamp = mktime($hours, $minutes);
  36.            
  37.             list($hours, $minutes) = explode(':', $time2);
  38.             $endTimestamp = mktime($hours, $minutes);
  39.            
  40.             $seconds = $endTimestamp - $startTimestamp;
  41.             $minutes = ($seconds / 60) % 60;
  42.             $hours = round($seconds / (60 * 60));
  43.            
  44.             //Siapin data hasil proses yg akan masuk ke database
  45.             $model->selisi_jam = $hours;
  46.             $model->selisi_menit = $minutes;
  47.            
  48.             if($model->save()){
  49.                 $this->redirect('index');
  50.             }  
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement