PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Savant/Savant2/Savant2_Plugin_dateformat.php

https://github.com/radicaldesigns/amp
PHP | 136 lines | 26 code | 14 blank | 96 comment | 6 complexity | 637a2929520db05eb6e33b28f416148e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * Base plugin class.
  4. */
  5. require_once 'Savant2/Plugin.php';
  6. /**
  7. *
  8. * Outputs a formatted date using strftime() conventions.
  9. *
  10. * $Id: Savant2_Plugin_dateformat.php,v 1.2 2004/12/08 01:37:21 pmjones Exp $
  11. *
  12. * @author Paul M. Jones <pmjones@ciaweb.net>
  13. *
  14. * @package Savant2
  15. *
  16. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU Lesser General Public License as
  20. * published by the Free Software Foundation; either version 2.1 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful, but
  24. * WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. */
  29. class Savant2_Plugin_dateformat extends Savant2_Plugin {
  30. /**
  31. *
  32. * The default strftime() format string to use for dates.
  33. *
  34. * You can preset the default format string via Savant::loadPlugin().
  35. *
  36. * $conf = array(
  37. * 'format' => '%Y-%m-%d %H:%M:%S'
  38. * );
  39. *
  40. * $Savant->loadPlugin('dateformat', $conf);
  41. *
  42. * ... and in your template, to use the default format string:
  43. *
  44. * $this->plugin('date', $datestring);
  45. *
  46. * ... or, to use a custom string at call-time:
  47. *
  48. * $this->plugin('date', $datestring, '%b');
  49. *
  50. * @access public
  51. *
  52. * @var string
  53. *
  54. */
  55. var $format = '%c';
  56. /**
  57. *
  58. * The default strftime() format string to use for dates.
  59. *
  60. * You can preset the custom format strings via Savant::loadPlugin().
  61. *
  62. * $conf = array(
  63. * 'custom' => array(
  64. * 'mydate' => '%Y-%m-%d',
  65. * 'mytime' => '%R'
  66. * )
  67. * );
  68. *
  69. * $Savant->loadPlugin('dateformat', $conf);
  70. *
  71. * ... and in your template, to use a preset custom string by name:
  72. *
  73. * $this->plugin('date', $datestring, 'mydate');
  74. *
  75. * @access public
  76. *
  77. * @var array
  78. *
  79. */
  80. var $custom = array(
  81. 'date' => '%Y-%m-%d',
  82. 'time' => '%H:%M:%S'
  83. );
  84. /**
  85. *
  86. * Outputs a formatted date using strftime() conventions.
  87. *
  88. * @access public
  89. *
  90. * @param string $datestring Any date-time string suitable for
  91. * strtotime().
  92. *
  93. * @param string $format The strftime() formatting string, or a named
  94. * custom string key from $this->custom.
  95. *
  96. * @return string
  97. *
  98. */
  99. function plugin($datestring, $format = null)
  100. {
  101. settype($format, 'string');
  102. // does the format string have a % sign in it?
  103. if (strpos($format, '%') === false) {
  104. // no, look for a custom format string
  105. if (isset($this->custom[$format])) {
  106. // found a custom format string
  107. $format = $this->custom[$format];
  108. } else {
  109. // did not find the custom format, revert to default
  110. $format = $this->format;
  111. }
  112. }
  113. // convert the date string to the specified format
  114. if (trim($datestring != '')) {
  115. return strftime($format, strtotime($datestring));
  116. } else {
  117. // no datestring, return VOID
  118. return;
  119. }
  120. }
  121. }
  122. ?>