Fix date formatting
[platal.git] / include / misc.inc.php
index 4f61049..c1b8c67 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -112,30 +112,6 @@ function replace_accent($string)
     return strtr($string, $uc_convert);
 }
 
-/** creates a username from a first and last name
- *
- * @param $prenom the firstname
- * @param $nom the last name
- *
- * return STRING the corresponding username
- */
-function make_username($prenom,$nom)
-{
-    /* on traite le prenom */
-    $prenomUS=replace_accent(trim($prenom));
-    $prenomUS=stripslashes($prenomUS);
-
-    /* on traite le nom */
-    $nomUS=replace_accent(trim($nom));
-    $nomUS=stripslashes($nomUS);
-
-    // calcul du login
-    $username = strtolower($prenomUS.".".$nomUS);
-    $username = str_replace(" ","-",$username);
-    $username = str_replace("'","",$username);
-    return $username;
-}
-
 /* Un soundex en français posté par Frédéric Bouchery
    Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/).
    C'est une bonne démonstration de la force des expressions régulières compatible Perl.
@@ -189,37 +165,6 @@ function soundex_fr($sIn)
     return substr( $sIn . '    ', 0, 4);
 }
 
-/** met les majuscules au debut de chaque atome du prénom
- * @param $prenom le prénom à formater
- * return STRING le prénom avec les majuscules
- */
-function make_firstname_case($prenom)
-{
-    $prenom = strtolower($prenom);
-    $pieces = explode('-',$prenom);
-
-    foreach ($pieces as $piece) {
-        $subpieces = explode("'",$piece);
-        $usubpieces="";
-        foreach ($subpieces as $subpiece)
-            $usubpieces[] = ucwords($subpiece);
-        $upieces[] = implode("'",$usubpieces);
-    }
-    return implode('-',$upieces);
-}
-
-
-function make_forlife($prenom, $nom, $promo)
-{
-    $prenomUS = replace_accent(trim($prenom));
-    $nomUS    = replace_accent(trim($nom));
-
-    $forlife = strtolower($prenomUS.".".$nomUS.".".$promo);
-    $forlife = str_replace(" ","-",$forlife);
-    $forlife = str_replace("'","",$forlife);
-    return $forlife;
-}
-
 /** Convert ip to uint (to store it in a database)
  */
 function ip_to_uint($ip)
@@ -244,5 +189,136 @@ function uint_to_ip($uint)
     return long2ip($uint);
 }
 
+/** Converts DateTime / string / timestamp to DateTime object
+ */
+function make_datetime($date)
+{
+    if ($date instanceof DateTime) {
+        return $date;
+    } elseif (preg_match('/^\d{14}$/', $date) || preg_match('/^\d{8}$/', $date)) {
+        return new DateTime($date);
+    } elseif (is_int($date) || is_numeric($date)) {
+        return new DateTime("@$date");
+    } else {
+        try {
+            $d = new DateTime($date);
+            return $d;
+        } catch (Exception $e) {
+            return null;
+        }
+    }
+}
+
+/** Here to allow clean date formats instead of PHP's erroneous system...
+ * Format :
+ * %a: Mon...Sun
+ * %A: Monday...Sunday
+ * %d: day, two digits
+ * %e: day, space before single digits
+ * %j: day of year
+ * %u: day of week (1 for monday, 7 for sunday)
+ * %w: day of week (0 for sunday, 6 for saturday)
+ *
+ * //%U: week number (first week is that with the first sunday)
+ * //%V: week number (ISO 8601-1988: first week is that with at least 4 week days)
+ * %W: week number (first week is that with the first monday)
+ *
+ * %b: Jan...Dec
+ * %B: January...December
+ * %h: = %b
+ * %m: month, two digits
+ *
+ * %C: century, two digits
+ * %g: year, two digits (ISO 8601-1988)
+ * %G: %g with four digits
+ * %y: year, two digits
+ * %Y: year, four digits
+ *
+ * %H: hour, two digits, 24h format
+ * %h: hour, two digits, 12h format
+ * %l: hour, two digits, space before single, 12h format
+ * %M: minute, two digits
+ * %p: AM/PM
+ * %P: am/pm
+ * %r: %I:%M:%S %p
+ * %R: %H:%M
+ * %S: second, two digits
+ * %T: %H:%M:%S
+ * %z: timezone (offset)
+ * %Z: timezone (abbrev)
+ *
+ * %x: %e %B %Y
+ * %X: %T
+ * %s: unix timestamp
+ * %%: %
+ */
+function format_datetime($date, $format)
+{
+    $format = str_replace(array('%X', '%x', '%R', '%r', '%T', '%%'),
+                     array('%T', '%e %B %Y', '%H:%M', '%I:%M:%S %p', '%H:%M:%S', '%%'),
+                     $format);
+
+    $date = make_datetime($date);
+    $yy = (int) $date->format('Y');
+//    if ($yy > 1901 && $yy < 2038) {
+//        return strftime($format, $date->format('U'));
+//    } else {
+        $w = (int) $date->format('w');
+        $u = $w;
+        if ($u == 0) {
+            $u = 7;
+        }
+        $weekday = new DateTime('2010-05-' . (10 + $u));
+        $aa = strftime('%A', $weekday->format('U'));
+        $a = strftime('%a', $weekday->format('U'));
+
+        $m = $date->format('m');
+        $monthday = new DateTime('2010-' . $m . '-01');
+        $bb = strftime('%B', $monthday->format('U'));
+        $b = strftime('%b', $monthday->format('U'));
+        $y = $date->format('y');
+
+        $j  = $date->format('z'); // Day of year
+        $d  = $date->format('d'); // Day of month, 2 digits
+        $e  = $date->format('j'); // Day of month, leanding space
+        if (strlen($e) == 1) {
+            $e = ' ' . $e;
+        }
+
+        $yy = "$yy";
+        $cc = substr($yy, 0, 2); // Century
+        $ww = $date->format('W'); // Week number
+
+        $hh = $date->format('H'); // Hour, 24h
+        $h  = $date->format('h'); // Hour, 12h
+        $l  = $date->format('g'); // Hour, 12h with leading space
+        if (strlen($l) == 1) {
+            $l = ' ' . $l;
+        }
+
+        $mm = $date->format('i'); // Minutes
+        $p  = $date->format('A'); // AM/PM
+        $pp = $date->format('a'); // am/pm
+        $ss = $date->format('s'); // Seconds
+
+        $s  = $date->format('U'); // Timestamp
+        $zz = $date->format('T'); // Timezone abbrev
+        $z  = $date->format('Z'); // Timezone offset
+
+        $txt = str_replace(
+            array('%a', '%A', '%d', '%e', '%j', '%u', '%w',
+                  '%W', '%b', '%B', '%h', '%m', '%C', '%y', '%Y',
+                  '%H', '%h', '%l', '%M', '%p', '%P', '%S', '%z', '%Z',
+                  '%%'),
+            array($a, $aa, $d, $e, $j, $u, $w,
+                  $ww, $b, $bb, $b, $m, $cc, $y, $yy,
+                  $hh, $h, $l, $mm, $p, $pp, $ss, $z, $zz,
+                  '%'),
+            $format);
+
+        return $txt;
+//    }
+}
+
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>