You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modifier.date_format.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty date_format modifier plugin
  10. * Type: modifier
  11. * Name: date_format
  12. * Purpose: format datestamps via strftime
  13. * Input:
  14. * - string: input date string
  15. * - format: strftime format for output
  16. * - default_date: default date if $string is empty
  17. *
  18. * @link http://www.smarty.net/manual/en/language.modifier.date.format.php date_format (Smarty online manual)
  19. * @author Monte Ohrt <monte at ohrt dot com>
  20. *
  21. * @param string $string input date string
  22. * @param string $format strftime format for output
  23. * @param string $default_date default date if $string is empty
  24. * @param string $formatter either 'strftime' or 'auto'
  25. *
  26. * @return string |void
  27. * @uses smarty_make_timestamp()
  28. */
  29. function smarty_modifier_date_format($string, $format = null, $default_date = '', $formatter = 'auto')
  30. {
  31. if ($format === null) {
  32. $format = Smarty::$_DATE_FORMAT;
  33. }
  34. /**
  35. * require_once the {@link shared.make_timestamp.php} plugin
  36. */
  37. static $is_loaded = false;
  38. if (!$is_loaded) {
  39. if (!is_callable('smarty_make_timestamp')) {
  40. require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
  41. }
  42. $is_loaded = true;
  43. }
  44. if ($string !== '' && $string !== '0000-00-00' && $string !== '0000-00-00 00:00:00') {
  45. $timestamp = smarty_make_timestamp($string);
  46. } elseif ($default_date !== '') {
  47. $timestamp = smarty_make_timestamp($default_date);
  48. } else {
  49. return;
  50. }
  51. if ($formatter === 'strftime' || ($formatter === 'auto' && strpos($format, '%') !== false)) {
  52. if (Smarty::$_IS_WINDOWS) {
  53. $_win_from = array('%D',
  54. '%h',
  55. '%n',
  56. '%r',
  57. '%R',
  58. '%t',
  59. '%T');
  60. $_win_to = array('%m/%d/%y',
  61. '%b',
  62. "\n",
  63. '%I:%M:%S %p',
  64. '%H:%M',
  65. "\t",
  66. '%H:%M:%S');
  67. if (strpos($format, '%e') !== false) {
  68. $_win_from[] = '%e';
  69. $_win_to[] = sprintf('%\' 2d', date('j', $timestamp));
  70. }
  71. if (strpos($format, '%l') !== false) {
  72. $_win_from[] = '%l';
  73. $_win_to[] = sprintf('%\' 2d', date('h', $timestamp));
  74. }
  75. $format = str_replace($_win_from, $_win_to, $format);
  76. }
  77. return strftime($format, $timestamp);
  78. } else {
  79. return date($format, $timestamp);
  80. }
  81. }