<div class="codetitle"><a style="CURSOR: pointer" data="36895" class="copybut" id="copybut36895" onclick="doCopy('code36895')"> 代码如下:<div class="codebody" id="code36895"> <?php echo time(); echo mktime(11,25,9,5,2010);//和time一样的 echo microtime(); echo mktime(0,1,1970); ?> 这里面输出分别是1283657290、1283657100、0.88533200 1283657290、-25200。从最后一个值看,我们知道这里返回的时间戳是经过了时区调整的,也就是我在中国的1970年1月1日0点,格林尼治还没有到0点呢,于是这里的时间会是负数且整好等于-83600。 再看 <div class="codetitle"><a style="CURSOR: pointer" data="28609" class="copybut" id="copybut28609" onclick="doCopy('code28609')"> 代码如下:<div class="codebody" id="code28609"> <?php echo date ("H i l d F",1283657100); echo gmdate("H i l d F",1283657100); echo strftime("%Hh%M %A %d %b",1283657100); //strftime()工作的方式和date()没有什么不同,除了特殊格式化字符的前面必须添加一个百分号%。 echo strtotime("2010-9-5 11:25:00"); var_dump(getdate (time())); ?> 这里的输出是11 25 Sunday 05 September、03 25 Sunday 05 September、11h25 Sunday 05 Sep、1283657100、array(11) { ["seconds"]=> int(9) ["minutes"]=> int(39) ["hours"]=> int(11) ["mday"]=> int(5) ["wday"]=> int(0) ["mon"]=> int(9) ["year"]=> int(2010) ["yday"]=> int(247) ["weekday"]=> string(6) "Sunday" ["month"]=> string(9) "September" [0]=> int(1283657949) } 主要看第三个输出,这里输出的是在中国的2010年9月5日11点25分的时候 格林尼治的时间是多少。这里也要计入时差的。而且这里还有一个很奇妙的就是只有gmdate没有发出警告,其余的都有警告说不能依赖于系统的时区。想象也是,因为gmdate算出来的只是格林尼治的时间,就算是系统时区错了,一加一减就又正常了。 <div class="codetitle"><a style="CURSOR: pointer" data="6583" class="copybut" id="copybut6583" onclick="doCopy('code6583')"> 代码如下:<div class="codebody" id="code6583"> //时间格式化 function sgmdate($dateformat,$timestamp='',$format=0) { global $_SCONFIG,$_SGLOBAL; if(empty($timestamp)) { $timestamp = $_SGLOBAL['timestamp']; } $timeoffset = strlen($_SGLOBAL['member']['timeoffset'])>0?intval($_SGLOBAL['member']['timeoffset']):intval($_SCONFIG['timeoffset']); $result = ''; if($format) { $time = $_SGLOBAL['timestamp'] - $timestamp; if($time > 243600) { $result = gmdate($dateformat,$timestamp + $timeoffset 3600); } elseif ($time > 3600) { $result = intval($time/3600).lang('hour').lang('before'); } elseif ($time > 60) { $result = intval($time/60).lang('minute').lang('before'); } elseif ($time > 0) { $result = $time.lang('second').lang('before'); } else { $result = lang('now'); } } else { $result = gmdate($dateformat,$timestamp + $timeoffset 3600); } return $result; } 我们直接看if($format){}里的东西,首先求得系统当前时间和我传进来的时间(一般是数据库里的时间,如2010-9-4 21:00:00)的差。如果时间差是在一天以内,则直接得出结论比如两小时前,如果是大于1天,则调用gmdate。这里我就是很搞不懂的。为什么要调用这个诡异的函数,而不是直接date($timestamp)呢?这他妈的到底是什么意思阿? gmdate When run in Finland (GMT +0200),the first line below prints "Jan 01 1998 00:00:00",while the second prints "Dec 31 1997 22:00:00". <div class="codetitle"><a style="CURSOR: pointer" data="46104" class="copybut" id="copybut46104" onclick="doCopy('code46104')"> 代码如下:<div class="codebody" id="code46104"> <?php echo date("M d Y H:i:s",mktime(0,1998)); echo gmdate("M d Y H:i:s",1998)); ?> 也就是说gmdate是考虑过了时差的。这里会输出标准的时间格式,而不是几天前。 接下来着重看uchome的function_common里的函数 <div class="codetitle"><a style="CURSOR: pointer" data="27541" class="copybut" id="copybut27541" onclick="doCopy('code27541')"> 代码如下:<div class="codebody" id="code27541"> //字符串时间化 function sstrtotime($string) { global $_SGLOBAL,$_SCONFIG; $time = ''; if($string) { $time = strtotime($string); if(gmdate('H:i',$_SGLOBAL['timestamp'] + $_SCONFIG['timeoffset'] 3600) != date('H:i',$_SGLOBAL['timestamp'])) { $time = $time - $_SCONFIG['timeoffset'] 3600; } } return $time; } 别看这个函数很少,但是看得我很吃力。这里是要把一个时间字符串转换为时间戳。比如我这里输入的是2010 9 4 21:08,则$time的值就是这个时间到January 1 1970 00:00:00 GMT的时间差,这里是要考虑什么时差的。$_SGLOBAL[''timestamp]的值其实和$time一样的方法算出来的,但是数值可能有细小的差别。$_SCONFIG['timeoffset']是在config的那个表里面的,目前其值为8。这里有一种情况是系统的时区是对的,或者是不对的,需要通过$_SCONFIG['timeoffset']来检验。gmdate将传入的时间戳(运行程序的地方的时间戳)经过系统的时区来得到此时格林尼治那个地方的时间。如果是系统时区设对了,那么这个正好一加一减,和后面的相等了(其实这里$_SGLOBAL[''timestamp]的数值是无关紧要的)。如果是正常的,那么就不需要改$time,如果是不正常的,那么需要减一下。但是还是那句话,这他妈的到底是什么意思阿? (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|