util.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * Funciones de utilidad
  4. */
  5. function fechaGuion($fechaTxt){//convierte fecha a guiones
  6. $fechaTxt = trim($fechaTxt);
  7. if(substr($fechaTxt,2,1) == "/" && substr($fechaTxt,5,1) == "/"){// dd/mm/aaaa
  8. $fechaArr = explode("/", $fechaTxt);
  9. return $fechaArr[2]."-".$fechaArr[1]."-".$fechaArr[0];
  10. }
  11. if(substr($fechaTxt,4,1) == "-" && substr($fechaTxt,7,1) == "-")// aaaa-mm-dd
  12. return $fechaTxt;
  13. return "";
  14. }
  15. function fechaSlash($fechaTxt){//convierte fecha a /
  16. $fechaTxt = trim($fechaTxt);
  17. if(substr($fechaTxt,2,1) == "/" && substr($fechaTxt,5,1) == "/"){// dd/mm/aaaa
  18. return $fechaTxt;
  19. }
  20. if(substr($fechaTxt,4,1) == "-" && substr($fechaTxt,7,1) == "-"){// aaaa-mm-dd
  21. $fechaArr = explode("-", $fechaTxt);
  22. return $fechaArr[2]."/".$fechaArr[1]."/".$fechaArr[0];
  23. }
  24. return "";
  25. }
  26. function fechaTexto($fechaTxt, $showYear = true){//convierte fecha a cadena de texto
  27. $fechaTxt = trim($fechaTxt);
  28. if(substr($fechaTxt,2,1) == "/" && substr($fechaTxt,5,1) == "/"){// dd/mm/aaaa
  29. $fechaArr = explode("/", $fechaTxt);
  30. if($showYear)
  31. return intval($fechaArr[0])." de ".mesNombre($fechaArr[1])." de ".$fechaArr[2];
  32. else
  33. return intval($fechaArr[0])." de ".mesNombre($fechaArr[1]);
  34. }
  35. if(substr($fechaTxt,4,1) == "-" && substr($fechaTxt,7,1) == "-"){// aaaa-mm-dd
  36. $fechaArr = explode("-", $fechaTxt);
  37. if($showYear)
  38. return intval($fechaArr[2])." de ".mesNombre($fechaArr[1])." de ".$fechaArr[0];
  39. else
  40. return intval($fechaArr[2])." de ".mesNombre($fechaArr[1]);
  41. }
  42. return "";
  43. }
  44. function mesNombre($num){
  45. $meses=array(1=>"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre");
  46. return $meses[intval($num)];
  47. }
  48. function diaNombre($num){
  49. $dias=array("domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado");
  50. return $dias[intval($num)];
  51. }
  52. function horaMin($arr, $campo = "Horario_hora"){
  53. $min = "";
  54. foreach($arr as $horario){
  55. if($min == "" || date('H:i', strtotime($horario[$campo])) < date('H:i', strtotime($min))){
  56. $min = $horario[$campo];
  57. }
  58. }
  59. return date('H:i', strtotime($min));
  60. }
  61. function horaMax($arr, $campo = "Horario_hora_final"){
  62. $max = "";
  63. foreach($arr as $horario){
  64. if($max == "" || date('H:i', strtotime($horario[$campo])) > date('H:i', strtotime($max))){
  65. $max = $horario[$campo];
  66. }
  67. }
  68. return date('H:i', strtotime($max));
  69. }
  70. function duracionMinutos($fechahora_i, $fechahora_f){
  71. return round((strtotime($fechahora_f) - strtotime($fechahora_i)) / 60,2);
  72. }
  73. function validaPassword($pass){
  74. $expr = '/^\S*(?=\S{5,})(?=\S*[a-zA-Z])(?=\S*[\d])(?=\S*[\W])\S*$/';
  75. return preg_match($expr, $pass);
  76. }