<?php
/**
* Class dateFunctions
*
* Ver. 1 20190726
*
* clase para trabajar con fechas
*/
class dateFunctions
{
public $config_meses=array(1=>"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
/**
* Funcion que devuelve una fecha en formato ingles: yyyy/mm/dd
*
* @param string $date - date in spanish format without time
*
* @return string - date in english format or ""
*/
public function dateSpanishToEnglish($date)
{
if ($date) {
$values=preg_split('/(\/|-)/', $date);
if (count($values)==3 && $this->validateDateEs($date)) {
return date("Y/m/d", mktime(0, 0, 0, $values[1], $values[0], $values[2]));
}
}
return "";
}
/**
* Funcion que devuelve una fecha en formato español: dd/mm/yyyy
*
* @param string $date - date in english format with or less time: yyyy/mm/dd, yyyy-mm-dd hh:mm:ss, ...
* @param boolean $showTime - define if show the time
*
* @return string - date in spanish format or ""
*/
public function dateEnglishToSpanish($date, $showTime=true)
{
if (strtotime($date)) {
if ($showTime) {
return date("d/m/Y H:i:s", strtotime($date));
}
return date("d/m/Y", strtotime($date));
}
return "";
}
/**
* Funcion para validar una fecha en formato español: dd/mm/yyyy, d/m/yyyy, d/m/yy
*
* @param string $date - in spanish format: dd/mm/yyyy, d/m/yyyy, d/m/yy
*
* @return boolean
*/
public function validateDateEs($date)
{
$pattern="/^(0?[1-9]|[12][0-9]|3[01])[\/|-](0?[1-9]|[1][012])[\/|-]((19|20)?[0-9]{2})$/";
if (preg_match($pattern, $date)) {
$values=preg_split("[\/|-]", $date);
if (checkdate($values[1], $values[0], $values[2])) {
return true;
}
}
return false;
}
/**
* Funcion para validar una fecha y hora en formato: dd/mm/yyyy hh:mm:ss, d/m/yyyy hh:mm:ss, d/m/yy hh:mm:ss
*
* @param string $dateTime - date time in format: dd/mm/yyyy hh:mm:ss, d/m/yyyy hh:mm:ss, d/m/yy hh:mm:ss
*
* @return boolean
*/
public function validateDateTimeEs($dateTime)
{
$valores=explode(" ", $dateTime);
if (count($valores)==2) {
if ($this->validateDateEs($valores[0]) && $this->validateTime($valores[1])) {
return true;
}
}
return false;
}
/**
* Funcion para validar una fecha en formato ingles: yyyy/mm/dd, yyyy/m/d, yy/m/d
*
* @param string $date - in english format: yyyy/mm/dd, yyyy/m/d, yy/m/d
*
* @return boolean
*/
public function validateDateEn($date)
{
$pattern="/^((19|20)?[0-9]{2})[\/|-](0?[1-9]|[1][012])[\/|-](0?[1-9]|[12][0-9]|3[01])$/";
if (preg_match($pattern, $date)) {
$values=preg_split("[\/|-]", $date);
if (checkdate($values[1], $values[2], $values[1]) || $date=="2016-02-29") {
return true;
}
}
return false;
}
/**
* Funcion para validar una fecha y hora en formato: yyyy/mm/dd hh:mm:ss, yyyy/m/d hh:mm:ss, yy/m/d hh:mm:ss
*
* @param string $dateTime - date time in format: yyyy/mm/dd hh:mm:ss, yyyy/m/d hh:mm:ss, yy/m/d hh:mm:ss
*
* @return boolean
*/
public function validateDateTimeEn($dateTime)
{
$valores=explode(" ", $dateTime);
if (count($valores)==2) {
if ($this->validateDateEn($valores[0]) && $this->validateTime($valores[1])) {
return true;
}
}
return false;
}
/**
* Funcion to check time in format: hh:mm:ss
*
* @param string $time - format hh:mm:ss
*
* @return boolean
*/
public function validateTime($time)
{
$pattern="/^([0-1][0-9]|[2][0-3])[\:]([0-5][0-9])[\:]([0-5][0-9])$/";
if (preg_match($pattern, $time)) {
return true;
}
return false;
}
/**
* Function to return the number of days between two dates
*
* Sample:
* "2010-10-10","2010-10-11" return 1
* "2010-10-10","2010-10-09" return -1
*
* @param string $date1 - date in yyyy-mm-dd format
* @param string $date2 - date in yyyy-mm-dd format
*
* @return int - diff days between $date1-date2. value can be negative.
*
* Note: if date1 or date2 are error, return 0
*/
public function daysBetweenTwoDates($date1,$date2)
{
if ($this->validateDateEn($date1)==false || $this->validateDateEn($date2)==false) {
return 0;
}
return ((strtotime($date2)-strtotime($date1))/86400);
}
/**
* Funcion que valida que una fecha sea correcta, y que este comprendida en un periodo máximo de un
* año desde la fecha actual. Si es inferior a la fecha actual devuelve false
*
* @param string $date - Tiene que recibir la fecha en formato español: dd/mm/yyyy
*
* @return boolean - Devuelve true|false
*/
public function validate_dateInOneYear($date)
{
if ($this->validateDateEs($date)) {
# pasamos la fecha a formato ingles
$date_Timestamp=strtotime($this->dateSpanishToEnglish($date));
$dateMorOneYear_Timestamp=strtotime("+1 year");
if ($date_Timestamp>$dateMorOneYear_Timestamp || $date_Timestamp<time()) {
return false;
}
return true;
}
return false;
}
/**
* Convert the date yyyy-mm-dd to "25 de marzo del 2015"
*
* @param date $date
*
* @return string - If $date is error, return ""
*/
public function dateToText($date)
{
$timestamp=strtotime($date);
if (!$timestamp) {
return "";
}
$day=date("j", $timestamp);
$month=date("n", $timestamp);
$year=date("Y", $timestamp);
return $day." de ".$this->config_meses[$month]." del ".$year;
}
/**
* Funcion que calcula la edad de una persona en relacion a su fecha de
* nacimiento.
*
* @param date $date - Tiene que recibir la fecha de nacimiento en formato YYYY-MM-DD
*
* @return int - years old or -1 if $date is incorrect
*/
public function getYearsOld($date)
{
try {
list($Y,$m,$d) = explode("-", $date);
return( date("md") < $m.$d ? date("Y")-$Y-1 : date("Y")-$Y );
} catch (Exception $e) {
return -1;
}
}
/**
* Function that return the min date in array
*
* @param array $dates - array with dates in format Y-m-d
*
* @return string - min date in format Y-m-d
*/
public function getMinDate($dates)
{
if (is_array($dates)==false || count($dates)==0) {
return "";
}
$result=array_map(
function ($el) {
return strtotime($el);
}, $dates
);
return date("Y-m-d", min($result));
}
}
Comentarios sobre la versión: Versión 1 (1)