/*! * GJframework * model/Datime.class.php * v1.2.0 (17/09/2019) * * Created by Guillaume Juncker on 24/08/2018. * Copyright © Guillaume Juncker. All rights reserved. */ /** * Datime * Classe de gestion du temps */ class Datime extends DateTime { public $year = "", $month = "", $day = "", $hour = "", $minute = "", $seconde = "", $quarter = "", $week = "", $weekday = "", $yearday = ""; const MONTHS_STR = array("long" => array(NULL, "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"), "short" => array(NULL, "Janv.", "Févr.", "Mars", "Avr.", "Mai", "Juin", "Juil.", "Août", "Sept.", "Oct.", "Nov.", "Déc.")), WEEKDAYS_STR = array("long" => array("dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"), "short" => array("dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam.", "dim.")), FORMAT_FR = "d/m/Y H:i:s", FORMAT_SQL = "Y-m-d H:i:s", FORMAT_FILE = "Y-m-d_H-i-s", FORMAT_DBB = "Y-m-d_W_H-i-s", FORMAT_DATE_FR = "d/m/Y", FORMAT_DATE_SQL = "Y-m-d", FORMAT_DATE_ICS = "Ymd"; /*------------------------------------------------------------------------------------------------------------------------------*/ /** * __construct * Construit une date * [@param string $date_time: la date et l'heure ou le timestamp] * [@param string $interval: un intervalle de date positif (+3d et/ou +1i) ou négatif (-3h et/ou -1y) à appliquer] * [@param string $timezone: la zone du fuseau horaire] * @return void */ public function __construct(string $date_time = "NOW", string $interval = NULL, string $timezone = "Europe/Paris") { // Création de l'objet DateTime $date_time = str_replace("/", "-", $date_time); parent::__construct($date_time, new DateTimeZone($timezone)); // Gestion des intervalles if($interval) { $this->interval($interval);} // Assemblage de l'ogjet $date_time = preg_split("/(-| |\.|:)/", $this->format("Y-m-d H:i:s W.N.z")); $this->year = $date_time[0]; $this->month = $date_time[1]; $this->day = $date_time[2]; $this->hour = $date_time[3]; $this->minute = $date_time[4]; $this->seconde = $date_time[5]; $this->quarter = ceil($date_time[1] / 3); $this->week = $date_time[6]; $this->weekday = $date_time[7]; $this->yearday = $date_time[8]; } /*------------------------------------------------------------------------------------------------------------------------------*/ /** * is_null * Détermine si une date existe * @return bool: TRUE si la date est NULL, FALSE sinon */ public function is_null() { return !$this->year OR $this->year== "0000"; } /** * interval * Permet d'ajouter ou de retirer une durée à la date courante * @param string $interval: un intervalle de date positif (+3d et/ou +1i) ou négatif (-3h et/ou -1y) à appliquer * @return void */ public function interval(string $interval) { $intervals = explode(" ", strtoupper($interval)); foreach($intervals as $interval) { $P_T = "P"; if(in_array(substr($interval, -1, 1), array("H", "I", "S"))) { $P_T.= "T"; $interval = str_replace("I", "M", $interval); } $DateInterval = new DateInterval($P_T.substr($interval, 1)); if($interval[0]== "+") { $this->add($DateInterval);} if($interval[0]== "-") { $this->sub($DateInterval);} } $this->__construct($this->format(self::FORMAT_SQL)); } /** * diff * Traduit la différence entre 2 dates * @param Datime|string $date: la date avec laquelle comparer l'objet courrant * [@param bool $absolute: true pour forcer l'intervalle à être positif] * @return string: la différence sous forme de texte intelligible */ public function diff($date, $absolute = NULL) { if(!($date instanceOf Datime)) { $date = new Datime($date);} $diff = parent::diff($date, $absolute); if($absolute!==NULL) { return $diff;} else { if(abs((new Datime())->getTimestamp() - $this->getTimestamp()) < 5) { if($diff->invert) { $in_ago = "Il y a ";} else { $in_ago = "Dans ";} } $diffs_str = array(); if($diff->y > 0) { if($diff->y== 1) { $diffs_str[] = "un an";} else { $diffs_str[] = $diff->y." ans";} } if($diff->m > 0) { if($diff->m== 1) { $diffs_str[] = "un mois";} else { $diffs_str[] = $diff->m." mois";} } if($diff->d > 0) { if($diff->d== 1) { $diffs_str[] = "un jour";} else { $diffs_str[] = $diff->d." jours";} } if($diff->h > 0) { if($diff->h== 1) { $diffs_str[] = "une heure";} else { $diffs_str[] = $diff->h." heures";} } if($diff->i > 0) { if($diff->i== 1) { $diffs_str[] = "une minute";} else { $diffs_str[] = $diff->i." minutes";} } if($diff->s > 0) { if($diff->s== 1) { $diffs_str[] = "une seconde";} else { $diffs_str[] = $diff->s." secondes";} } $diff_str = ""; if($diffs_str[0]) { $diff_str.= $diffs_str[0];} if($diffs_str[1]) { $diff_str.= " et ".$diffs_str[1];} return $in_ago.$diff_str; } } /** * month * Retourne le nom correspondant au mois numérique renseigné * [@param string $format: le format de retour du nom de mois (long pour "Janvier", short pour "Janv.")] * [@param int $month: le mois de l'année au format numérique (de 1 pour "Janvier" à 12 pour "Décembre")] * @return string: le nom du mois dans le format choisi */ public final function month(string $format = "long", int $month = NULL) { if(!$month) { $month = (int) $this->month;} return self::MONTHS_STR[$format][$month]; } /** * weekday * Retourne le nom correspondant au jour de la semaine numérique renseigné * [@param string $format: le format de retour du nom du jour de la semaine (long pour "dimanche", short pour "dim.")] * [@param int $weekday: le jour de la semaine au format numérique (de 0 pour "dimanche" à 6 pour "samedi")] * @return string: le nom du jour de la semaine dans le format choisi */ public final function weekday(string $format = "long", int $weekday = NULL) { if(!$weekday) { $weekday = (int) $this->weekday;} return self::WEEKDAYS_STR[$format][$weekday]; } /** * toString * Retourne une date au format textuel tel que dimanche 18 Octobre 1992 * [@param string $format: le format de retour de la date (long pour "dimanche 18 Octobre 1992", short pour "dim. 18 Oct. 1992")] * [@param bool $relative: TRUE pour avoir la date relative à aujourd'hui] * @return string: la date au format texte */ public final function toString(string $format = "long", bool $relative = TRUE) { if($this->is_null()) { return "date inconnue";} // Gestion de la relativité à maintenant if($relative) { $today = new Datime("00:00:00"); $diff = $today->diff($this->format(self::FORMAT_DATE_SQL), FALSE); $diff->invert ? $diff_days = -$diff->days : $diff_days = $diff->days; switch($diff_days) { case -1: $str = "hier"; break; case 0: $str = "aujourd'hui"; break; case 1: $str = "demain"; break; default: return $this->toString($format, FALSE); break; } } // Gestion de la non relativité else { $str = $this->weekday($format)." ".$this->day." ".$this->month($format)." ".$this->year;} // Ajout éventuel de l'heure if(!($this->hour== 0 AND $this->minute== 0 AND $this->seconde== 0)) { $str.= $this->format(" à H\hi"); } return $str; } /*------------------------------------------------------------------------------------------------------------------------------*/ public function __toString() { return $this->toString(); } }