phpDocumentor lodel
[ class tree: lodel ] [ index: lodel ] [ all elements ]

Source for file date.php

Documentation is available at date.php

  1. <?php
  2. /**
  3.  * Fichier utilitaire de gestion des dates
  4.  *
  5.  * PHP versions 4 et 5
  6.  *
  7.  * LODEL - Logiciel d'Edition ELectronique.
  8.  *
  9.  * Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
  10.  * Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  11.  * Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  12.  * Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  13.  * Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  14.  * Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  15.  *
  16.  * Home page: http://www.lodel.org
  17.  *
  18.  * E-Mail: lodel@lodel.org
  19.  *
  20.  * All Rights Reserved
  21.  *
  22.  * This program is free software; you can redistribute it and/or modify
  23.  * it under the terms of the GNU General Public License as published by
  24.  * the Free Software Foundation; either version 2 of the License, or
  25.  * (at your option) any later version.
  26.  *
  27.  * This program is distributed in the hope that it will be useful,
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  * GNU General Public License for more details.
  31.  *
  32.  * You should have received a copy of the GNU General Public License
  33.  * along with this program; if not, write to the Free Software
  34.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  * @author Ghislain Picard
  37.  * @author Jean Lamy
  38.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  39.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  40.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  41.  * @licence http://www.gnu.org/copyleft/gpl.html
  42.  * @version CVS:$Id:
  43.  * @package lodel
  44.  */
  45.  
  46.  
  47. /**
  48.  * Converti une date humaine en date mysql
  49.  *
  50.  * Cette fonction accepte diverses formats de date :
  51.  * - jj/mm/aaaa
  52.  * - jj mm aaaa
  53.  * - jj.mm.aaaa
  54.  * - jj-mm-aaaa
  55.  *
  56.  * @param string $s la date 'humaine'
  57.  * @return string la date transformée en format mySQL
  58.  */
  59. function mysqldate($s$type)
  60. {
  61.     //what is the delimiting character? (support space, slash, dash, point) 
  62.     $s trim($s);
  63.     if($type == 'time'{
  64.         if (strpos($s':'0{
  65.             $delimiter ':';
  66.         }    elseif (strpos($s'h'0{
  67.             $delimiter 'h';
  68.         }    elseif (strpos($s'H'0{
  69.             $delimiter 'H';
  70.         }
  71.     else {
  72.         if (strpos($s'/'0{
  73.             $delimiter "\/";
  74.         }    elseif (strpos($s'-'0{
  75.             $delimiter '-';
  76.         }    elseif (strpos($s'.'0{
  77.             $delimiter '.';
  78.         }    elseif (strpos($s' '0{
  79.             $delimiter ' ';
  80.         }
  81.     }
  82.     if (!$delimiter{
  83.         if (strlen($s== && is_numeric($s)) // une année seulement
  84.             return $s '-00-00';
  85.         elseif(strlen($s0{
  86.             return "bad date";
  87.         else 
  88.             return ''
  89.         }
  90.     }
  91.     if(preg_match("`^\d\d\d\d.\d\d.\d\d$`"$s)) 
  92.         list ($y$m$dpreg_split("/s*$delimiter+/"$s);
  93.     else
  94.         list ($d$m$ypreg_split("/s*$delimiter+/"$s);
  95.     $d intval(trim($d));
  96.  
  97.     if ((($d || $d 31&& !preg_match("`[:hH-]`"$delimiter))) {
  98.         return 'bad date';
  99.     }
  100.     $m trim($m);
  101.  
  102.     if($type != 'time'{
  103.         if (intval($m== 0{
  104.             $m mois($m);
  105.         }
  106.         if ($m == 0{
  107.             return '';
  108.         }
  109.     
  110.         if (!isset ($y)) // la date n'a pas ete mise
  111.             $today getdate(time());
  112.             $y $today['year']// cette annee
  113.             if ($m $today['mon']{
  114.                 $y ++// ou l'annee prochaine
  115.             }
  116.         }
  117.     
  118.         $y intval(trim($y));
  119.     
  120.         //the last value is always the year, so check it for 2- to 4-digit convertion 
  121.         if (intval($y100)    {
  122.             $y += 2000;
  123.         }
  124.     
  125.         if (!checkdate($m$d$y)) {
  126.             return '';
  127.         }
  128.     
  129.         if ($d 10 && strlen($d== 1)    {
  130.             $d "0$d";
  131.         }
  132.         if ($m 10 && strlen($m== 1)    {
  133.             $m "0$m";
  134.         }
  135.         return "$y-$m-$d";
  136.     }
  137.     else {
  138.         if(!isset($y))
  139.             $y '00';
  140.         return $d.":".$m.":".$y;
  141.     }
  142. }
  143.  
  144.  
  145. /**
  146.  * Retourne le chiffre du mois par rapport à son nom
  147.  *
  148.  * @param string le nom du mois
  149.  * @return integer le numéro du mois
  150.  */
  151. function mois($m)
  152. {
  153.     $m strtolower(utf8_decode($m));
  154.  
  155.     switch (substr($m03))    {
  156.     case "jan" :
  157.         return 1;
  158.     case "fev" :
  159.         return 2;
  160.     case "fv" :
  161.         return 2;
  162.     case "fév" :
  163.         return 2;
  164.     case "mar" :
  165.         return 3;
  166.     case "avr" :
  167.         return 4;
  168.     case "mai" :
  169.         return 5;
  170.     case "aou" :
  171.         return 8;
  172.     case "ao" :
  173.         return 8;
  174.     case "aoû" :
  175.         return 8;
  176.     case "sep" :
  177.         return 9;
  178.     case "oct" :
  179.         return 10;
  180.     case "nov" :
  181.         return 11;
  182.     case "dec" :
  183.         return 12;
  184.     case "déc" :
  185.         return 12;
  186.     case "dc" :
  187.         return 12;
  188.     }
  189.     switch (substr($m04)) {
  190.     case "juin" :
  191.         return 6;
  192.     case "juil" :
  193.         return 7;
  194.     }
  195.     return 0;
  196. }
  197.  
  198. /**
  199.  * Transforme une date avec heure dans le format 'datetime' de MySQL
  200.  *
  201.  * @param string $s la date
  202.  * @param string $type le type de format dans lequel transformer la date donnée. Par défaut
  203.  *  'datetime'
  204.  * @return string la date transformée
  205.  */
  206. function mysqldatetime($s$type 'datetime')
  207. {
  208.     $s trim(stripslashes($s));
  209.     if (!$s{
  210.         return '';
  211.     }
  212.  
  213.     if ($s == 'aujourd\'hui' || $s == 'today' || $s == 'maintenant' || $s == 'now'{
  214.         $timestamp time();
  215.     }    elseif ($s == 'hier' || $s == 'yesterday'{
  216.         $arr localtime(time()1);
  217.         $timestamp mktime($arr['tm_hour']$arr['tm_min']$arr['tm_sec']$arr['tm_mon'1$arr['tm_mday'11900 $arr['tm_year']);
  218.     elseif ($s == 'demain' || $s == 'tomorrow')    {
  219.         $arr localtime(time()1);
  220.         $timestamp mktime($arr['tm_hour']$arr['tm_min']$arr['tm_sec']$arr['tm_mon'1$arr['tm_mday'11900 $arr['tm_year']);
  221.     }    elseif (preg_match("/^\s*(dans|il y a)\s+(\d+)\s*(an|mois|jour|heure|minute)s?\s*$/i"$s$result)) {
  222.         $val $result[1== 'dans' $result[2: - $result[2];
  223.         $arr localtime(time()1);
  224.         switch ($result[3]{
  225.         case 'an' :
  226.             $arr['tm_year'+= $val;
  227.             break;
  228.         case 'mois' :
  229.             $arr['tm_mon'+= $val;
  230.             break;
  231.         case 'jour' :
  232.             $arr['tm_mday'+= $val;
  233.             break;
  234.         case 'heure' :
  235.             $arr['tm_hour'+= $val;
  236.             break;
  237.         case 'minute' :
  238.             $arr['tm_min'+= $val;
  239.             break;
  240.         }
  241.  
  242.         $timestamp mktime($arr['tm_hour']$arr['tm_min']$arr['tm_sec']$arr['tm_mon'1$arr['tm_mday']1900 $arr['tm_year']);
  243.  
  244.     }    else {
  245.         $date mysqldate($s$type);
  246.  
  247.         if($date == "bad date")
  248.             return $type;
  249.         elseif($type == 'time' && $date)
  250.             return $date;
  251.         elseif(!$date && $type != 'time')
  252.             $date date("Y-m-d");
  253.         elseif(!$date)
  254.             $date date("H:i:s");
  255.  
  256.         list ($y$m$dexplode('-'$date);
  257.  
  258.         if ($type == "date"{
  259.             return $date;
  260.         }
  261.  
  262.         if (preg_match("/(\d+)[:hH](?:(\d+)(?:[:](\d+))?)?\s*$/"$s$result)) // time
  263.             $timestamp mktime($result[1]$result[2]$result[3]$m$d$y);
  264.             if ($timestamp <= 0// no algebra    
  265.                 $time sprintf("%02d:%02d:%02d"$result[1]$result[2]$result[3]);
  266.             }
  267.         }    else {
  268.             $arr localtime();
  269.             $timestamp mktime($arr['tm_hour']$arr['tm_min']$arr['tm_sec']$m$d$y);
  270.             if ($timestamp <= 0// no algebra
  271.                 $time sprintf("%02d:%02d:%02d"$arr['tm_hour']$arr['tm_min']$arr['tm_sec']);
  272.             }
  273.         }
  274.     }
  275.     if ($timestamp <= && $time{
  276.         if ($type == 'datetime' && $date{
  277.             return trim($date.' '.$time);
  278.         }
  279.         return '';
  280.     }
  281.  
  282.     if ($type == 'date'{
  283.         return date('Y-m-d'$timestamp);
  284.     }    elseif ($type == 'datetime'{
  285.         return date('Y-m-d H:i:s'$timestamp);
  286.     }    elseif ($type == 'time'{
  287.         return date('H:i:s'$timestamp);
  288.     }    elseif ($type == 'timestamp'{
  289.         return $timestamp;
  290.     }    else {
  291.         die('type inconnu dans mysqldatetime');
  292.     }
  293. }
  294. ?>

Documentation generated on Thu, 10 Jul 2008 05:07:27 +0200 by phpDocumentor 1.4.0a2