shared.make_timestamp.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * Smarty shared plugin
  4. * @package Smarty
  5. * @subpackage PluginsShared
  6. */
  7. /**
  8. * Function: smarty_make_timestamp<br>
  9. * Purpose: used by other smarty functions to make a timestamp
  10. * from a string.
  11. * @author Monte Ohrt <monte at ohrt dot com>
  12. * @param string $string
  13. * @return string
  14. */
  15. function smarty_make_timestamp($string)
  16. {
  17. if(empty($string)) {
  18. // use "now":
  19. return time();
  20. } elseif ($string instanceof DateTime) {
  21. return $string->getTimestamp();
  22. } elseif (preg_match('/^\d{14}$/', $string)) {
  23. // it is mysql timestamp format of YYYYMMDDHHMMSS?
  24. return mktime(substr($string, 8, 2),substr($string, 10, 2),substr($string, 12, 2),
  25. substr($string, 4, 2),substr($string, 6, 2),substr($string, 0, 4));
  26. } elseif (is_numeric($string)) {
  27. // it is a numeric string, we handle it as timestamp
  28. return (int)$string;
  29. } else {
  30. // strtotime should handle it
  31. $time = strtotime($string);
  32. if ($time == -1 || $time === false) {
  33. // strtotime() was not able to parse $string, use "now":
  34. return time();
  35. }
  36. return $time;
  37. }
  38. }
  39. ?>