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