fork(1) download
  1. <?php
  2.  
  3. // Creamos el objeto instanciando la clase Timefull en la variable
  4. $ingreso_tiempo = new Timefull;
  5.  
  6. //Llamamos a la funcion, colocando los parametros correctos
  7. echo $ingreso_tiempo->getTime('5 hour','secs');
  8. // Resultado -> 18000 seconds
  9.  
  10. //Si no se especifica el parametro de la unidad a convertir, se autoasignara uno que por defecto son segundos
  11. echo $ingreso_tiempo->getTime('1 hour');
  12. // Resultado -> 3600 seconds
  13.  
  14. //Es posible colocar prefijos para las unidades de tiempo y no es preciso colocar en singular o plural los mismas, un ejemplo:
  15. echo $ingreso_tiempo->getTime('3601 mins','hour');
  16. // Resultado -> 60 hours 1 minute
  17.  
  18. //Para obtener directamente la operacion de conversion a la unidad de tiempo especificada, debera colocarse como tercer parametro el boolean 'true', aqui un ejemplo:
  19. echo $ingreso_tiempo->getTime('3601 mins','hour',true);
  20. // Resultado -> 60.01667 hours
  21.  
  22. //Si se ingresa una unidad cuyo valor no es igual o mayor al valor que definiria la siguiente unidad a esta, la funcion traera como resultado el mismo valor sin transformarse:
  23.  
  24. //Dado que 45 segundos no es el valor que tiene un minuto entonces la funcion traera los 45 segundos ingresados
  25. echo $ingreso_tiempo->getTime('45 secs','minutes');
  26. //Resultado-> 45 seconds;
  27.  
  28. //Si se desea obtener sin importar el valor de la unidad ingresada, el valor de la unidad a transformar, colocar como tercer parametro el boolean 'true'
  29. echo $ingreso_tiempo->getTime('45 secs','minutes',true);
  30. //Resultado-> 0.75000 minutes
  31.  
  32. // Otros ejemplos
  33.  
  34. echo $ingreso_tiempo->getTime('4564 seconds','hours');
  35. // Resultado -> 1 hour 16 minutes 4 seconds
  36.  
  37. //Recordar que los parametros a ingresar como unidades de tiempo deben ser en ingles y estos pueden ser secs o seconds (singular o plural) para segundos, mins o minutes (singular o plural) para minutos y hour o hours (singular o plural) para horas.
  38.  
  39. /************************************************************************************************
  40.  
  41. CODIGO FUENTE DE LA CLASE
  42.  
  43. *************************************************************************************************/
  44.  
  45. class Timefull
  46. {
  47.  
  48. public $def_return = ['s'=>'second','m'=>'minute','h'=>'hour','seconds','minutes','hours','min','sec','mins','secs'];
  49. public $def_time;
  50. public $def_transform;
  51. public $time;
  52. public $seconds;
  53. public $minutes;
  54. public $hours;
  55. public $error;
  56. private $raw;
  57.  
  58. public function getTime($time = null,$def_transform = null,$raw = false){
  59. $this->raw = ($raw) ? true : false ;
  60. if ($time != null){
  61. $this->time = trim($time);
  62. return $this->evaluateTime($def_transform);
  63. }
  64. }
  65.  
  66. public function evaluateTime($def_transform){
  67. if ($this->time){
  68. $split = str_split($this->time);
  69. $this->time = null;
  70. for ($i=0,$s=count($split); $i<$s;$i++){
  71. if( is_numeric($split[$i])){
  72. $this->time .= $split[$i];
  73. } else if (($split[$i] == ',') || ($split[$i] == '.')) {
  74. $this->time .= str_replace(',','.',$split[$i]);
  75. } else {
  76. break;
  77. }
  78. }
  79. $this->def_time = substr(implode('',$split),$i);
  80. return $this->transformTime($def_transform);
  81. }
  82. }
  83.  
  84. public function transformTime($def_transform){
  85. if($def_transform){
  86. $def_transform = (in_array($def_transform,$this->def_return)) ? $def_transform : $this->autoDef(true) ;
  87. } else {
  88. $def_transform = $this->autoDef();
  89. }
  90. if ($this->error) { return $this->error; exit; }
  91. $this->def_transform = $def_transform;
  92. $time_transform = $this->evaluateTransform();
  93. if(is_array($time_transform['time'])){
  94. for ($i=0,$s=count($time_transform['time']); $i<$s;$i++) {
  95. $return[] = ($time_transform['time'][$i] > 1) ? "{$time_transform['time'][$i]} {$time_transform['def'][$i]}s " : "{$time_transform['time'][$i]} {$time_transform['def'][$i]} " ;
  96. }
  97. return implode('',$return);
  98. } else {
  99. if ($time_transform['time'] > 1 || $time_transform['time'] > 0.1){
  100. return "{$time_transform['time']} {$time_transform['def']}s";
  101. } else {
  102. return "{$time_transform['time']} {$time_transform['def']}";
  103. }
  104. }
  105. }
  106.  
  107. public function evaluateTransform(){
  108. if (preg_match("/\bsec(s?|ond(s?))?\b/i",$this->def_time)) { return $this->seconds(); }
  109. if (preg_match("/\bmin(s?|ute(s?))?\b/i",$this->def_time)) { return $this->minutes(); }
  110. if (preg_match("/\bhour(s?)\b/i",$this->def_time)) { return $this->hours(); }
  111. }
  112.  
  113. public function seconds($retry = false){
  114. if ((preg_match("/\bsec(s?|ond(s?))?\b/i",$this->def_transform)) || $retry){
  115. $this->seconds = $this->time;
  116. return ['time'=>$this->seconds,'def'=>$this->def_return['s']];
  117. } else {
  118. if(preg_match("/\bmin(s?|ute(s?))?\b/i",$this->def_transform)) return $this->secondsMinutes();
  119. if(preg_match("/\bhour(s?)\b/i",$this->def_transform)) return $this->secondsHours();
  120. }
  121. }
  122.  
  123. public function minutes($retry = false){
  124. if ((preg_match("/\bmin(s?|ute(s?))?\b/i",$this->def_transform)) || $retry){
  125. $this->minutes = $this->time;
  126. return ['time'=>$this->minutes,'def'=>$this->def_return['m']];
  127. } else {
  128. if(preg_match("/\bsec(s?|ond(s?))?\b/i",$this->def_transform)) return $this->minutesSeconds();
  129. if(preg_match("/\bhour(s?)\b/i",$this->def_transform)) return $this->minutesHours();
  130. }
  131. }
  132.  
  133. public function hours(){
  134. if (preg_match("/\bhour(s?)\b/i",$this->def_transform)){
  135. $this->hours = $this->time;
  136. return ['time'=>$this->hours,'def'=>$this->def_return['h']];
  137. } else {
  138. if(preg_match("/\bsec(s?|ond(s?))?\b/i",$this->def_transform)) return $this->hoursSeconds();
  139. if(preg_match("/\bmin(s?|ute(s?))?\b/i",$this->def_transform)) return $this->hoursMinutes();
  140. }
  141. }
  142.  
  143. public function secondsMinutes(){
  144. $time = $this->time;
  145. if(!$this->raw){
  146. if ($time > 59){
  147. $this->minutes = (int)floor($time/60);
  148. $this->seconds = (int)$time-($this->minutes*60);
  149. if ($this->seconds == 0){
  150. return ['time'=>$this->minutes,$this->def_return['m']];
  151. } else {
  152. return ['time'=>[$this->minutes,$this->seconds],'def'=>[$this->def_return['m'],$this->def_return['s']]];
  153. }
  154. } else {
  155. return $this->seconds(true);
  156. }
  157. } else {
  158. $this->minutes = number_format($time/60,5,'.','');
  159. if (substr(strstr($this->minutes,'.'),1) == 0) {
  160. $this->minutes = (int)floor($time/60);
  161. return ['time'=>$this->minutes,'def'=>$this->def_return['m']];
  162. } else {
  163. return ['time'=>$this->minutes,'def'=>$this->def_return['m']];
  164. }
  165. }
  166. }
  167.  
  168. public function secondsHours(){
  169. $time = $this->time;
  170. if(!$this->raw){
  171. if ($time > 59 && $time < 3600){
  172. return $this->secondsMinutes();
  173. } else if ($time > 3599){
  174. $this->hours = (int)floor($time/3600);
  175. $this->minutes = (int)floor(($time-($this->hours*3600))/60);
  176. $this->seconds = (int)(($time-($this->hours*3600))-($this->minutes*60));
  177. if($this->minutes == 0 && $this->seconds == 0){
  178. return ['time'=>$this->hours,'def'=> $this->def_return['h']];
  179. } else if ($this->minutes != 0 && $this->seconds == 0) {
  180. return ['time'=>[$this->hours,$this->minutes],'def'=> [$this->def_return['h'],$this->def_return['m']]];
  181. } else if ($this->minutes == 0 && $this->seconds != 0) {
  182. return ['time'=>[$this->hours,$this->seconds],'def'=> [$this->def_return['h'],$this->def_return['s']]];
  183. } else {
  184. return ['time'=>[$this->hours,$this->minutes,$this->seconds],'def'=> [$this->def_return['h'],$this->def_return['m'],$this->def_return['s']]];
  185. }
  186. } else {
  187. return $this->seconds(true);
  188. }
  189. } else {
  190. $this->hours = number_format($time/3600,5,'.','');
  191. if (substr(strstr($this->hours,'.'),1) == 0) {
  192. $this->hours = (int)floor($time/3600);
  193. return ['time'=>$this->hours,'def'=>$this->def_return['h']];
  194. } else {
  195. return ['time'=>$this->hours,'def'=>$this->def_return['h']];
  196. }
  197. }
  198. }
  199.  
  200. public function minutesSeconds(){
  201. $time = $this->time;
  202. if (!$this->raw){
  203. $this->seconds = (int)($time*60);
  204. return ['time'=>$this->seconds,'def'=>$this->def_return['s']];
  205. } else {
  206. $this->seconds = ($time*60);
  207. return ['time'=>$this->seconds,'def'=>$this->def_return['s']];
  208. }
  209. }
  210.  
  211. public function minutesHours(){
  212. $time=$this->time;
  213. if(!$this->raw){
  214. if ($time > 59) {
  215. $this->hours = (int)floor($time/60);
  216. $this->minutes = (int)floor($time-($this->hours*60));
  217. if($this->minutes == 0){
  218. return ['time'=>$this->hours,'def'=>$this->def_return['h']];
  219. } else {
  220. return ['time'=>[$this->hours,$this->minutes],'def'=>[$this->def_return['h'],$this->def_return['m']]];
  221. }
  222. } else {
  223. return $this->minutes(true);
  224. }
  225. } else {
  226. $this->hours = number_format($time/60,5,'.','');
  227. if (substr(strstr($this->hours,'.'),1) == 0) {
  228. $this->hours = (int)floor($time/60);
  229. return ['time'=>$this->hours,'def'=>$this->def_return['h']];
  230. } else {
  231. return ['time'=>$this->hours,'def'=>$this->def_return['h']];
  232. }
  233. }
  234. }
  235.  
  236. public function hoursSeconds(){
  237. $time = $this->time;
  238. if(!$this->raw){
  239. $this->seconds = (int)($time*3600);
  240. return ['time'=>$this->seconds,'def'=>$this->def_return['s']];
  241. } else {
  242. $this->seconds = ($time*3600);
  243. return ['time'=>$this->seconds,'def'=>$this->def_return['s']];
  244. }
  245. }
  246.  
  247. public function hoursMinutes(){
  248. $time = $this->time;
  249. if(!$this->raw){
  250. $this->minutes = (int)($time*60);
  251. return ['time'=>$this->minutes,'def'=>$this->def_return['m']];
  252. } else {
  253. $this->minutes = ($time*60);
  254. return ['time'=>$this->minutes,'def'=>$this->def_return['m']];
  255. }
  256. }
  257.  
  258. public function autoDef($error = false){
  259. if ($error) {
  260. $this->error = 'IS NOT A CORRECT VALUE TO TRANSFORM!';
  261. } else {
  262. return 'seconds';
  263. }
  264. }
  265.  
  266. }
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
18000 seconds3600 seconds60 hours 1 minute 60.01667 hours45 seconds0.75000 minutes1 hour 16 minutes 4 seconds