WordPress纯代码给博客添加运行天数代码

WordPress纯代码给博客添加运行天数代码,又开始折腾了,今天出一个WordPress纯代码给博客添加运行天数代码,很多人看到别人网站底部写着运行了多少天,觉得很心仪,wordpress怎么添加这个功能。其实非常简单,添加一段代码就可以实现,下面给大家出个教程。

WordPress纯代码给博客添加运行天数:PHP版

将下列代码放到网站的footer或header中

<span class="pun">&lt;?</span><span class="pln">php
</span><span class="com">/**
* 秒转时间,格式 年 月 日 时 分 秒
* 
* @author wangyupeng129@126.com
* @param int $time
* @return array|boolean
*/</span>
<span class="com">// 设置时区 </span><span class="pln">
date_default_timezone_set</span><span class="pun">(</span><span class="str">'Asia/Shanghai'</span><span class="pun">);</span>
<span class="kwd">function</span> <span class="typ">Sec2Time</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">){</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">is_numeric</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">)){</span><span class="pln">
$value </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">(</span>
<span class="str">"years"</span> <span class="pun">=&gt;</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"days"</span> <span class="pun">=&gt;</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"hours"</span> <span class="pun">=&gt;</span> <span class="lit">0</span><span class="pun">,</span>
<span class="str">"minutes"</span> <span class="pun">=&gt;</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"seconds"</span> <span class="pun">=&gt;</span> <span class="lit">0</span><span class="pun">,</span>
<span class="pun">);</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">$time </span><span class="pun">&gt;=</span> <span class="lit">31556926</span><span class="pun">){</span><span class="pln">
$value</span><span class="pun">[</span><span class="str">"years"</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> floor</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">/</span><span class="lit">31556926</span><span class="pun">);</span><span class="pln">
$time </span><span class="pun">=</span> <span class="pun">(</span><span class="pln">$time</span><span class="pun">%</span><span class="lit">31556926</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">$time </span><span class="pun">&gt;=</span> <span class="lit">86400</span><span class="pun">){</span><span class="pln">
$value</span><span class="pun">[</span><span class="str">"days"</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> floor</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">/</span><span class="lit">86400</span><span class="pun">);</span><span class="pln">
$time </span><span class="pun">=</span> <span class="pun">(</span><span class="pln">$time</span><span class="pun">%</span><span class="lit">86400</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">$time </span><span class="pun">&gt;=</span> <span class="lit">3600</span><span class="pun">){</span><span class="pln">
$value</span><span class="pun">[</span><span class="str">"hours"</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> floor</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">/</span><span class="lit">3600</span><span class="pun">);</span><span class="pln">
$time </span><span class="pun">=</span> <span class="pun">(</span><span class="pln">$time</span><span class="pun">%</span><span class="lit">3600</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">$time </span><span class="pun">&gt;=</span> <span class="lit">60</span><span class="pun">){</span><span class="pln">
$value</span><span class="pun">[</span><span class="str">"minutes"</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> floor</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">/</span><span class="lit">60</span><span class="pun">);</span><span class="pln">
$time </span><span class="pun">=</span> <span class="pun">(</span><span class="pln">$time</span><span class="pun">%</span><span class="lit">60</span><span class="pun">);</span>
<span class="pun">}</span><span class="pln">
$value</span><span class="pun">[</span><span class="str">"seconds"</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> floor</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">);</span>
<span class="kwd">return</span> <span class="pun">(</span><span class="pln">array</span><span class="pun">)</span><span class="pln"> $value</span><span class="pun">;</span>
<span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span>
<span class="kwd">return</span> <span class="pun">(</span><span class="kwd">bool</span><span class="pun">)</span><span class="pln"> FALSE</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pun">}</span>
<span class="com">// 本站创建的时间</span><span class="pln">
$site_create_time </span><span class="pun">=</span><span class="pln"> strtotime</span><span class="pun">(</span><span class="str">'2017-07-14 00:00:00'</span><span class="pun">);</span><span class="pln">
$time </span><span class="pun">=</span><span class="pln"> time</span><span class="pun">()</span> <span class="pun">-</span><span class="pln"> $site_create_time</span><span class="pun">;</span><span class="pln">
$uptime </span><span class="pun">=</span> <span class="typ">Sec2Time</span><span class="pun">(</span><span class="pln">$time</span><span class="pun">);</span>
<span class="pun">?&gt;</span>

然后下列代码插入统计代码当中或网站合适的位置(一般在底部)即可。

<span class="pun">本站运行:</span><span class="hljs-tag"><span class="pun">&lt;</span><span class="hljs-name"><span class="pln">span</span></span> <span class="hljs-attr"><span class="pln">style</span></span><span class="pun">=</span><span class="hljs-string"><span class="str">"color:red;"</span></span><span class="pun">&gt;</span>
</span><span class="php"><span class="hljs-meta"><span class="pun">&lt;?</span><span class="pln">php</span></span> <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $uptime</span><span class="pun">[</span><span class="hljs-string"><span class="str">'years'</span></span><span class="pun">];</span> <span class="hljs-meta"><span class="pun">?&gt;</span></span></span><span class="pun">年</span>
<span class="php"><span class="hljs-meta"><span class="pun">&lt;?</span><span class="pln">php</span></span> <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $uptime</span><span class="pun">[</span><span class="hljs-string"><span class="str">'days'</span></span><span class="pun">];</span> <span class="hljs-meta"><span class="pun">?&gt;</span></span></span><span class="pun">天</span>
<span class="php"><span class="hljs-meta"><span class="pun">&lt;?</span><span class="pln">php</span></span> <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $uptime</span><span class="pun">[</span><span class="hljs-string"><span class="str">'hours'</span></span><span class="pun">];</span> <span class="hljs-meta"><span class="pun">?&gt;</span></span></span><span class="pun">小时</span>
<span class="php"><span class="hljs-meta"><span class="pun">&lt;?</span><span class="pln">php</span></span> <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $uptime</span><span class="pun">[</span><span class="hljs-string"><span class="str">'minutes'</span></span><span class="pun">];</span> <span class="hljs-meta"><span class="pun">?&gt;</span></span></span><span class="pun">分</span>
<span class="php"><span class="hljs-meta"><span class="pun">&lt;?</span><span class="pln">php</span></span> <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $uptime</span><span class="pun">[</span><span class="hljs-string"><span class="str">'seconds'</span></span><span class="pun">];</span> <span class="hljs-meta"><span class="pun">?&gt;</span></span></span><span class="pun">秒</span>
<span class="hljs-tag"><span class="pun">&lt;/</span><span class="hljs-name"><span class="pln">span</span></span><span class="pun">&gt;</span></span>

WordPress纯代码给博客添加运行天数:js版

将下列代码放到网站的footer或header中

<span class="tag">&lt;script&gt;</span>
<span class="kwd">function</span><span class="pln"> secondToDate</span><span class="pun">(</span><span class="pln">second</span><span class="pun">)</span> <span class="pun">{</span>
<span class="kwd">if</span> <span class="pun">(!</span><span class="pln">second</span><span class="pun">)</span> <span class="pun">{</span>
<span class="kwd">return</span> <span class="lit">0</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">var</span><span class="pln"> time </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Array</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">);</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">second </span><span class="pun">&gt;=</span> <span class="lit">365</span> <span class="pun">*</span> <span class="lit">24</span> <span class="pun">*</span> <span class="lit">3600</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
time</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> parseInt</span><span class="pun">(</span><span class="pln">second </span><span class="pun">/</span> <span class="pun">(</span><span class="lit">365</span> <span class="pun">*</span> <span class="lit">24</span> <span class="pun">*</span> <span class="lit">3600</span><span class="pun">));</span><span class="pln">
second </span><span class="pun">%=</span> <span class="lit">365</span> <span class="pun">*</span> <span class="lit">24</span> <span class="pun">*</span> <span class="lit">3600</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">second </span><span class="pun">&gt;=</span> <span class="lit">24</span> <span class="pun">*</span> <span class="lit">3600</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
time</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> parseInt</span><span class="pun">(</span><span class="pln">second </span><span class="pun">/</span> <span class="pun">(</span><span class="lit">24</span> <span class="pun">*</span> <span class="lit">3600</span><span class="pun">));</span><span class="pln">
second </span><span class="pun">%=</span> <span class="lit">24</span> <span class="pun">*</span> <span class="lit">3600</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">second </span><span class="pun">&gt;=</span> <span class="lit">3600</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
time</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> parseInt</span><span class="pun">(</span><span class="pln">second </span><span class="pun">/</span> <span class="lit">3600</span><span class="pun">);</span><span class="pln">
second </span><span class="pun">%=</span> <span class="lit">3600</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">second </span><span class="pun">&gt;=</span> <span class="lit">60</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
time</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> parseInt</span><span class="pun">(</span><span class="pln">second </span><span class="pun">/</span> <span class="lit">60</span><span class="pun">);</span><span class="pln">
second </span><span class="pun">%=</span> <span class="lit">60</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">second </span><span class="pun">&gt;</span> <span class="lit">0</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
time</span><span class="pun">[</span><span class="lit">4</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> second</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">return</span><span class="pln"> time</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="tag">&lt;/script&gt;</span>
<span class="tag">&lt;script</span> <span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span> <span class="atn">language</span><span class="pun">=</span><span class="atv">"javascript"</span><span class="tag">&gt;</span>
<span class="kwd">function</span><span class="pln"> setTime</span><span class="pun">()</span> <span class="pun">{</span>
<span class="com">// 博客创建时间秒数,时间格式中,月比较特殊,是从0开始的,所以想要显示5月,得写4才行,如下</span>
<span class="kwd">var</span><span class="pln"> create_time </span><span class="pun">=</span> <span class="typ">Math</span><span class="pun">.</span><span class="pln">round</span><span class="pun">(</span><span class="kwd">new</span> <span class="typ">Date</span><span class="pun">(</span><span class="typ">Date</span><span class="pun">.</span><span class="pln">UTC</span><span class="pun">(</span><span class="lit">2017</span><span class="pun">,</span> <span class="lit">6</span><span class="pun">,</span> <span class="lit">14</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">))</span>
<span class="pun">.</span><span class="pln">getTime</span><span class="pun">()</span> <span class="pun">/</span> <span class="lit">1000</span><span class="pun">);</span>
<span class="com">// 当前时间秒数,增加时区的差异</span>
<span class="kwd">var</span><span class="pln"> timestamp </span><span class="pun">=</span> <span class="typ">Math</span><span class="pun">.</span><span class="pln">round</span><span class="pun">((</span><span class="kwd">new</span> <span class="typ">Date</span><span class="pun">().</span><span class="pln">getTime</span><span class="pun">()</span> <span class="pun">+</span> <span class="lit">8</span> <span class="pun">*</span> <span class="lit">60</span> <span class="pun">*</span> <span class="lit">60</span> <span class="pun">*</span> <span class="lit">1000</span><span class="pun">)</span> <span class="pun">/</span> <span class="lit">1000</span><span class="pun">);</span><span class="pln">
currentTime </span><span class="pun">=</span><span class="pln"> secondToDate</span><span class="pun">((</span><span class="pln">timestamp </span><span class="pun">-</span><span class="pln"> create_time</span><span class="pun">));</span><span class="pln">
currentTimeHtml </span><span class="pun">=</span><span class="pln"> currentTime</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span> <span class="pun">+</span> <span class="str">'年'</span> <span class="pun">+</span><span class="pln"> currentTime</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span> <span class="pun">+</span> <span class="str">'天'</span>
<span class="pun">+</span><span class="pln"> currentTime</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]</span> <span class="pun">+</span> <span class="str">'时'</span> <span class="pun">+</span><span class="pln"> currentTime</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]</span> <span class="pun">+</span> <span class="str">'分'</span> <span class="pun">+</span><span class="pln"> currentTime</span><span class="pun">[</span><span class="lit">4</span><span class="pun">]</span>
<span class="pun">+</span> <span class="str">'秒'</span><span class="pun">;</span><span class="pln">
document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"htmer_time"</span><span class="pun">).</span><span class="pln">innerHTML </span><span class="pun">=</span><span class="pln"> currentTimeHtml</span><span class="pun">;</span>
<span class="pun">}</span><span class="pln">
setInterval</span><span class="pun">(</span><span class="pln">setTime</span><span class="pun">,</span> <span class="lit">1000</span><span class="pun">);</span>
<span class="tag">&lt;/script&gt;</span>

然后下列代码插入统计代码当中或网站合适的位置(一般在底部)即可。

<span class="pun">网站运行:</span><span class="hljs-tag"><span class="pun">&lt;</span><span class="hljs-name"><span class="pln">span</span></span> <span class="hljs-attr"><span class="pln">id</span></span><span class="pun">=</span><span class="hljs-string"><span class="str">"htmer_time"</span></span> <span class="hljs-attr"><span class="pln">style</span></span><span class="pun">=</span><span class="hljs-string"><span class="str">"color: red;"</span></span><span class="pun">&gt;</span></span><span class="hljs-tag"><span class="pun">&lt;/</span><span class="hljs-name"><span class="pln">span</span></span><span class="pun">&gt;</span></span>
WordPress教程

WordPress给个人博客增加总访问量教程

2021-4-21 13:20:14

WordPress教程

#B2主题美化#WordPress B2主题设置下载面板默认值教程

2021-5-2 2:42:31

无法充值请加客服QQ3635339338人工充值
24小时自助充值教程QQ3635339338人工充值
版权声明 本站所发布的一切与破解相关的文章仅限用于学习和研究目的,请在下载后的24个小时之内,从您的电脑中彻底删除。所有资源均来自于网络,不得用于商业或者非法用途,否则一切后果请用户自负。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索