Source for file date.php
Documentation is available at date.php
* Fichier utilitaire de gestion des dates
* LODEL - Logiciel d'Edition ELectronique.
* Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
* Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
* Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
* Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* Home page: http://www.lodel.org
* E-Mail: lodel@lodel.org
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* @author Ghislain Picard
* @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* @licence http://www.gnu.org/copyleft/gpl.html
* Converti une date humaine en date mysql
* Cette fonction accepte diverses formats de date :
* @param string $s la date 'humaine'
* @return string la date transformée en format mySQL
//what is the delimiting character? (support space, slash, dash, point)
} elseif (strpos($s, 'h') > 0) {
} elseif (strpos($s, 'H') > 0) {
} elseif (strpos($s, '-') > 0) {
} elseif (strpos($s, '.') > 0) {
} elseif (strpos($s, ' ') > 0) {
list ($y, $m, $d) = preg_split("/s*$delimiter+/", $s);
list ($d, $m, $y) = preg_split("/s*$delimiter+/", $s);
if ((($d < 1 || $d > 31) && !preg_match("`[:hH-]`", $delimiter))) {
if (!isset ($y)) { // la date n'a pas ete mise
$y = $today['year']; // cette annee
if ($m < $today['mon']) {
$y ++ ; // ou l'annee prochaine
//the last value is always the year, so check it for 2- to 4-digit convertion
if ($d < 10 && strlen($d) == 1) {
if ($m < 10 && strlen($m) == 1) {
* Retourne le chiffre du mois par rapport à son nom
* @param string le nom du mois
* @return integer le numéro du mois
* Transforme une date avec heure dans le format 'datetime' de MySQL
* @param string $s la date
* @param string $type le type de format dans lequel transformer la date donnée. Par défaut
* @return string la date transformée
if ($s == 'aujourd\'hui' || $s == 'today' || $s == 'maintenant' || $s == 'now') {
} elseif ($s == 'hier' || $s == 'yesterday') {
$timestamp = mktime($arr['tm_hour'], $arr['tm_min'], $arr['tm_sec'], $arr['tm_mon'] + 1, $arr['tm_mday'] - 1, 1900 + $arr['tm_year']);
} elseif ($s == 'demain' || $s == 'tomorrow') {
$timestamp = mktime($arr['tm_hour'], $arr['tm_min'], $arr['tm_sec'], $arr['tm_mon'] + 1, $arr['tm_mday'] + 1, 1900 + $arr['tm_year']);
} elseif (preg_match("/^\s*(dans|il y a)\s+(\d+)\s*(an|mois|jour|heure|minute)s?\s*$/i", $s, $result)) {
$val = $result[1] == 'dans' ? $result[2] : - $result[2];
$timestamp = mktime($arr['tm_hour'], $arr['tm_min'], $arr['tm_sec'], $arr['tm_mon'] + 1, $arr['tm_mday'], 1900 + $arr['tm_year']);
elseif($type == 'time' && $date)
elseif(!$date && $type != 'time')
list ($y, $m, $d) = explode('-', $date);
if (preg_match("/(\d+)[:hH](?:(\d+)(?:[:](\d+))?)?\s*$/", $s, $result)) { // time
$timestamp = mktime($result[1], $result[2], $result[3], $m, $d, $y);
if ($timestamp <= 0) { // no algebra
$time = sprintf("%02d:%02d:%02d", $result[1], $result[2], $result[3]);
$timestamp = mktime($arr['tm_hour'], $arr['tm_min'], $arr['tm_sec'], $m, $d, $y);
if ($timestamp <= 0) { // no algebra
$time = sprintf("%02d:%02d:%02d", $arr['tm_hour'], $arr['tm_min'], $arr['tm_sec']);
if ($timestamp <= 0 && $time) {
if ($type == 'datetime' && $date) {
return trim($date. ' '. $time);
return date('Y-m-d', $timestamp);
} elseif ($type == 'datetime') {
return date('Y-m-d H:i:s', $timestamp);
} elseif ($type == 'time') {
return date('H:i:s', $timestamp);
} elseif ($type == 'timestamp') {
die('type inconnu dans mysqldatetime');
|