|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
lsemel Smarty Rookie
Joined: 18 Aug 2003 Posts: 5
|
Posted: Fri Sep 05, 2003 3:58 pm Post subject: smarty_make_timestamp fails for certain times |
|
|
The plugin shared.make_timestamp.php will fail if passed a Unix epoch seconds number in 1970:
Example:
Code: |
<?
echo date("r",smarty_make_timestamp(mktime(5,0,0,1,1,1970))); # shows a date in 2007
echo date("r",smarty_make_timestamp(mktime(5,0,0,1,1,1971))); # shows correct date
function smarty_make_timestamp($string)
{
if(empty($string)) {
$string = "now";
}
$time = strtotime($string);
if (is_numeric($time) && $time != -1)
return $time;
// is mysql timestamp format of YYYYMMDDHHMMSS?
if (is_numeric($string) && strlen($string) == 14) {
$time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),
substr($string,4,2),substr($string,6,2),substr($string,0,4));
return $time;
}
// couldn't recognize it, try to return a time
$time = (int) $string;
if ($time > 0)
return $time;
else
return time();
}
?> |
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Sep 05, 2003 5:18 pm Post subject: |
|
|
make_timestamp doesn't work reliable for very low timestamps because
php's strtotime doesn't work reliable with short timestamps. at least that's the result of this thread:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=223#1334
your first example ( mktime(5,0,0,1,1,1970) ) leads to timestamp 14400.
my strtotime parses year=1 month=44 day=0 and flattens that to year=4 month=7 day=31 so on my machine it becomes "Sat, 31 Jul 2004".
if you don't need the mysql-timestamp magic of date-format but need a reliable date-format for low timestamps it's easy to write your own little timestamp_format-modifer with php's strftime() |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|