shared.make_timestamp.php 1.3 KB

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