Leya 发表于 2010-1-1 02:41:54

PHP格式的UNIX时间戳转换为Javascript

<?
echo "本机所在的时区:".date("T")."<BR>";
echo "PHP时间戳(UNIX秒):".mktime()."<BR>";
echo "PHP时间(本地格式化后):".date("Y-m-d H:i:s")."<HR>";
?>
<script language="">
<!--
var d;
function myDate(timestamp) {
d = new Date(timestamp);
var jstimestamp = (d.getFullYear())+"-"+(d.getMonth()+1)+"-"+(d.getDate())+" "+(d.getHours())+":"+(d.getMinutes())+":"+(d.getSeconds());
return jstimestamp;
}
var phptimestamp = <?=mktime()?>;
var jstimestamp = myDate((phptimestamp*1000));

document.writeln("PHP传递给时间:" + phptimestamp + "<BR>");
alert("PHP传递给时间:" + phptimestamp);

document.writeln("将PHP传递来的时间乘以1000后的时间戳:" + phptimestamp*1000 + "<BR>");
alert("将PHP传递来的时间乘以1000后的时间戳:" + phptimestamp*1000);

document.writeln("自身格式化PHP传递来乘以1000后的时间戳:" + d + "<BR>");
alert("自身格式化PHP传递来乘以1000后的时间戳:" + d);

document.writeln("转换后的时间:" + jstimestamp + "<BR>");
alert("转换后的时间:" + jstimestamp);
//-->
</script>

   //计算日期的前N天或后N天,   
function   post_date($date,   $days)   {   
$t1   =   strtotime($date);   
$t2   =   $t1   -   $days*3600*24;   
return   date("Y-m-d",   $t2);   
}   
echo   post_date(date("Y-m-d"),2), "昨天的昨天<br>";
echo   post_date(date("Y-m-d"),1), "昨天<br>";
echo   post_date(date("Y-m-d"),0), "今天<br>"   ;
echo   post_date(date("Y-m-d"),-1), "明天<br>"   ;
echo   post_date(date("Y-m-d"),-2), "明天的明天<br>";echo   '方案1:'.date("Y-m-d",strtotime("+1   day"))."<br>";   
echo   '方案2:'.date("Y-m-d",time()+3600*24)."<br>";
页: [1]
查看完整版本: PHP格式的UNIX时间戳转换为Javascript