Skip to content

Instantly share code, notes, and snippets.

@inferno7291
Last active February 27, 2018 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inferno7291/ad7cb19c757a79e870dbb573bd8889d5 to your computer and use it in GitHub Desktop.
Save inferno7291/ad7cb19c757a79e870dbb573bd8889d5 to your computer and use it in GitHub Desktop.
Functions for date on PHP
/************************
* FUNCTION DATE
************************/
/**
* Devolver la fecha de hoy con o sin hora
*/
function dateToday($format = 'Y-m-d',$time = false){
$date = new DateTime();
return $time ? $date->format($format.' H:i:s') : $date->format($format);
}
/**
* Validar la fecha
*/
function dateValid($date,$format = 'd/m/Y'){
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
/**
* Dar formato a la fecha
*/
function dateFormat($date,$format = 'Y-m-d',$formatd = 'd/m/Y'){
if(!empty($date)):
$format = empty($format) ? 'Y-m-d': $format;
$d = DateTime::createFromFormat($format, $date);
return $d->format($formatd);
endif;
}
/**
* Cmabiar el format de la fecha para mysql
*/
function dateFormatMysql($fecha,$time = false,$comillas = true){
if(!empty($fecha)):
if($time):
$a = explode(' ',$fecha);
$b = implode("-", array_reverse(explode("/", $a[0])));
$date = $b.' '.$a[1];
else:
$date = implode("-", array_reverse(explode("/", $fecha)));
endif;
if($comillas)
$date = "'".$date."'";
return $date;
else:
return 'NULL';
endif;
}
/**
* Saber el ultimo dia del mes
*/
function lastDayMonth($fecha){
$fecha = new DateTime($fecha);
$fecha->modify('last day of this month');
return $fecha->format('d');
}
/**
* Saber dias entre 2 fechas
*/
function dateDiff($fecha,$fecha2 = NULL,$format = '%a'){
$f = new Datetime($fecha);
$f2 = new Datetime($fecha2);
$intval = $f->diff($f2);
return $intval->format($format);
}
/**
* A partir del numero de semana saber las fechas
* @param int $week_num [Numero de semana]
* @param int $year [Año de la semana]
* @return array [Devuelve las fechas de la semana]
*/
function weekNumberToDate($week_num,$year = false){
$year = $year ? $year : date('Y') ;
$current_date = new DateTime();
$current_date->setISODate($year,$week_num);
$week_start = $current_date->format('Y-m-d');
$current_date->setISODate($year,$week_num,7);
$week_end = $current_date->format('Y-m-d');
return weekDays($week_start,$week_end);
}
/**
* Saber los dias entre 2 fechas
* @param string $date_start [Fecha incial]
* @param string $date_end [Fecha final]
* @return array [Array con los dias entre las fechas]
*/
function weekDays($date_start,$date_end){
$date_start = new DateTime($date_start);
$date_end = new DateTime($date_end);
if($date_start < $date_end):
$day_dif = ($date_start->diff($date_end))->format('%a');
$dates = array($date_start->format('Y-m-d'));
for ($i=0; $i < $day_dif; $i++) {
$dates[] = $date_start->modify("+ 1 day")->format('Y-m-d');
}
return $dates;
else:
return 'La fecha incial es superior a la fecha final';
endif;
}
/**
* Saber el dia de la semana ej: 1=Lunes, 7=Domingo
*/
function dateKnowDay($fecha){
$fechats = strtotime($fecha);
return date('N', $fechats);
}
/**
* Dia de la semana
* @param optional $dia
*
* @return si se le pasa el parametro devuelve el dia, si no devuelve un array con todos los dias
*/
function dayOfWeek($dia = false){
$dias = array(1 => 'Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo');
if($dia)
return $dias[(int) $dia];
else
return $dias;
}
/**
* Mese/s del año
* @param optional $mes
*
* @return si se le pasa el parametro devuelve el mes, si no devuelve un array con todos los meses
*/
function monthsOfYear($mes = false){
$meses = array(1 => 'Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
if($mes)
return $meses[(int) $mes];
else
return $meses;
}
/**
* Diferencia entre años
*
* @param $y_start año desede
* @param $y_end año hasta
*
* @return array años
*/
function yearsDiff($y_start,$y_end){
$years = array();
if(is_numeric($y_start) && is_numeric($y_end)):
if($y_start <= $y_end):
for($i = $y_start; $i <= $y_end; $i++):
$years[] = $i;
endfor;
endif;
endif;
return $years;
}
/**
* Convertir hora a segundos
*
* @param string hora format H:i:s minimo hay que pasarle la hora
*/
function convertirHoraSegundos($hora){
$h = explode(':',$hora);
$hseg = $h[0]*3600;
$mseg = isset($h[1]) ? $h[1]*60: 00 ;
$seg = isset($h[2]) ? $h[2]: 00 ;
return $hseg + $mseg + $seg;
}
/**
* Pasar de segundos a H:i:s
*/
function convertirSegundosHora($segundos) {
$horas = floor($segundos/3600);
$minutos = str_pad(floor(($segundos-($horas*3600))/60),2,0,STR_PAD_LEFT);
$segundo = str_pad($segundos-($horas*3600)-($minutos*60),2,0,STR_PAD_LEFT);
return $horas.':'.$minutos.':'.$segundo;
}
/**
* Pasar de segundos a D:H:i:s
*/
function segundosToDHMS($segs){
$cadena = '';
if($segs >= 86400) {
$dias = floor($segs/86400);
$segs = $segs%86400;
$cadena = $dias.' día';
if($dias != 1) $cadena .= 's';
if($segs >= 0) $cadena .= ', ';
}
if($segs >= 3600){
$horas = floor($segs/3600);
$segs = $segs%3600;
$cadena .= $horas.' hora';
if($horas != 1) $cadena .= 's';
if($segs >= 0) $cadena .= ', ';
}
if($segs >= 60){
$minutes = floor($segs/60);
$segs = $segs%60;
$cadena .= $minutes.' minuto';
if($minutes != 1) $cadena .= 's';
if($segs >= 0) $cadena .= ', ';
}
$cadena .= $segs.' segundo';
if($segs != 1) $cadena .= 's';
return $cadena;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment