modifier.date_format.php 1.9 KB

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