modifier.date_format.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Include the {@link shared.make_timestamp.php} plugin
  9. */
  10. require_once $smarty->_get_plugin_filepath('shared', 'make_timestamp');
  11. /**
  12. * Smarty date_format modifier plugin
  13. *
  14. * Type: modifier<br>
  15. * Name: date_format<br>
  16. * Purpose: format datestamps via strftime<br>
  17. * Input:<br>
  18. * - string: input date string
  19. * - format: strftime format for output
  20. * - default_date: default date if $string is empty
  21. * @link http://smarty.php.net/manual/en/language.modifier.date.format.php
  22. * date_format (Smarty online manual)
  23. * @author Monte Ohrt <monte at ohrt dot com>
  24. * @param string
  25. * @param string
  26. * @param string
  27. * @return string|void
  28. * @uses smarty_make_timestamp()
  29. */
  30. function smarty_modifier_date_format($string, $format = '%b %e, %Y', $default_date = '')
  31. {
  32. if ($string != '') {
  33. $timestamp = smarty_make_timestamp($string);
  34. } elseif ($default_date != '') {
  35. $timestamp = smarty_make_timestamp($default_date);
  36. } else {
  37. return;
  38. }
  39. if (DIRECTORY_SEPARATOR == '\\') {
  40. $_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T');
  41. $_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
  42. if (strpos($format, '%e') !== false) {
  43. $_win_from[] = '%e';
  44. $_win_to[] = sprintf('%\' 2d', date('j', $timestamp));
  45. }
  46. if (strpos($format, '%l') !== false) {
  47. $_win_from[] = '%l';
  48. $_win_to[] = sprintf('%\' 2d', date('h', $timestamp));
  49. }
  50. $format = str_replace($_win_from, $_win_to, $format);
  51. }
  52. return strftime($format, $timestamp);
  53. }
  54. /* vim: set expandtab: */
  55. ?>