Fix date formatting
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Wed, 26 May 2010 08:26:02 +0000 (10:26 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Wed, 26 May 2010 08:28:00 +0000 (10:28 +0200)
PHP has a libc-like formatting, which support only dates between 1901 and
2038 ; and another one, which doesn't support that formatting...

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
include/misc.inc.php
plugins/modifier.date_format.php

index 6e4314b..c1b8c67 100644 (file)
@@ -209,5 +209,116 @@ function make_datetime($date)
     }
 }
 
+/** 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:
 ?>
index a2e5bec..b49b5d5 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+// %x stands for 15 mai 2010, %X for 12:14:26
 function smarty_modifier_date_format($string, $format = '%x', $default_date=null)
 {
     $d = empty($string) ? $default_date : $string;
     if (empty($format) || preg_match('/^[ 0\-]*$/', $d)) return;
-    $f = str_replace('%X', '%T', str_replace('%x', '%e %B %Y', $format));
 
-    $d = make_datetime($d);
-    return $d->format($f);
+    return format_datetime($d, $format);
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: