Last active
August 29, 2015 14:03
-
-
Save fastcodecoq/f9b914ffbe928cb2929d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // String cuando_paso | |
| // params | |
| // int time <tiempo unix> | |
| function cuando_paso (time) | |
| { | |
| var time = Math.floor( (new Date().getTime() / 1000) - Math.floor(time / 1000)); // obtenemos el tiempo actual | |
| var tiempos = { | |
| 31536000 : 'año', | |
| 2592000 : 'mes', | |
| 604800 : 'semana', | |
| 86400 : 'día', | |
| 3600 : 'hora', | |
| 60 : 'minuto', | |
| 1 : 'segundo' | |
| }; | |
| var postfijos = { | |
| 'año' : 's', | |
| 'mes' : 'es', | |
| 'semana' : 's', | |
| 'día' : 's', | |
| 'hora' : 's', | |
| 'minuto' : 's', | |
| 'segundo' : 's' | |
| }; | |
| for (unidad in tiempos) { | |
| if (time < Math.floor(unidad)){ | |
| var hubo = Math.floor(time / unidad); | |
| return hubo + ' ' + tiempos[unidad] + ((hubo>1)? postfijos[tiempos[unidad]] : '' ); | |
| } | |
| } | |
| } | |
| //llamado cuando_paso((new Date().getTime()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // String cuando_paso | |
| // params | |
| // int $time <tiempo unix> | |
| function cuando_paso ($time) | |
| { | |
| $time = time() - $time; // obtenemos el tiempo actual | |
| $tiempos = array ( | |
| 31536000 => 'año', | |
| 2592000 => 'mes', | |
| 604800 => 'semana', | |
| 86400 => 'día', | |
| 3600 => 'hora', | |
| 60 => 'minuto', | |
| 1 => 'segudno' | |
| ); | |
| $postfijos = array ( | |
| 'año' => 's', | |
| 'mes' => 'es', | |
| 'semana' => 's', | |
| 'día' => 's', | |
| 'hora' => 's', | |
| 'minuto' => 's', | |
| 'segundo' => 's' | |
| ); | |
| foreach ($tiempos as $unidad => $fracion) { | |
| if ($time < $unidad) continue; | |
| $hubo = floor($time / $unidad); | |
| return $hubo.' '.$fracion.(($hubo>1)? $postfijos[$fracion] : '' ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment