Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2.  
  3. //By Dominic Gannaway
  4.  
  5. /**
  6.  * Check they're set and valid strings
  7.  * @param string $time1
  8.  * @param string $time2
  9.  *  
  10.  * @return boolean
  11.  */
  12. function validateTimestamps($time1, $time2) {
  13.  
  14.     return isset($time1) && is_string($time1) && isset($time2) && is_string($time2);
  15. }
  16.  
  17. /**
  18.  * PHP 5.2 method - plus it's using math
  19.  * @param string $time1
  20.  * @param string $time2
  21.  *
  22.  * @return int
  23.  */
  24. function getTimeBetween($time1, $time2) {
  25.    
  26.     //check the times are valid
  27.     if(validateTimestamps($time1, $time2)) {
  28.     //convert to PHP timestamp
  29.         $time1 = strtotime($time1);
  30.         $time2 = strtotime($time2);
  31.     } else {
  32.         return 'You need to enter some timestamps';
  33.     }
  34.  
  35.     //find the difference of the two times, taking into consideration the absolute value
  36.     $diff = abs($time1 - $time2);
  37.    
  38.     //now we need to calculate the minutes, rounding down
  39.     $diff_minutes = floor($diff / 60);
  40.    
  41.     //return minutes
  42.     return $diff_minutes;
  43. }
  44.  
  45.  
  46. /**
  47.  * PHP 5.3 method - using the standard DateTime component
  48.  * @param string $time1
  49.  * @param string $time2
  50.  *
  51.  * @return int  
  52.  */
  53. function getTimeBetween2($time1, $time2) {
  54.    
  55.     //check the times are valid
  56.     if(validateTimestamps($time1, $time2)) {           
  57.         $start = new DateTime($time1);
  58.         $end = new DateTime($time2);
  59.     } else {
  60.         return 'You need to enter some timestamps';
  61.     }
  62.        
  63.     $diff = $start->diff($end);
  64.     //now we have the diff in segments, we need to put them back together
  65.     $diff_minutes = (($diff->days * 24 * 60) + ($diff->h * 60) + $diff->i);
  66.    
  67.     //return minutes
  68.     return $diff_minutes;
  69. }
  70.  
  71.  
  72. //should be 60 minutes
  73. echo getTimeBetween('Thu, 21 Dec 2000 16:01:07 +0200', 'Thu, 21 Dec 2000 17:01:07 +0200') . ' minutes<br>';
  74. echo getTimeBetween2('Thu, 21 Dec 2000 16:01:07 +0200', 'Thu, 21 Dec 2000 17:01:07 +0200') . ' minutes<br>';
  75.  
  76. //should be 120 minutes
  77. echo getTimeBetween('Thu, 21 Dec 2000 16:01:07 +0200', 'Thu, 21 Dec 2000 19:01:07 +0300') . ' minutes<br>';
  78. echo getTimeBetween2('Thu, 21 Dec 2000 16:01:07 +0200', 'Thu, 21 Dec 2000 19:01:07 +0300') . ' minutes<br>';
  79.  
  80. //should be 6310860 minutes
  81. echo getTimeBetween('Thu, 7 Dec 2000 16:01:07 +0200', 'Thu, 6 Dec 2012 19:01:07 -0800') . ' minutes<br>';
  82. echo getTimeBetween2('Thu, 7 Dec 2000 16:01:07 +0200', 'Thu, 6 Dec 2012 19:01:07 -0800') . ' minutes<br>';
  83.  
  84.  
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement