Heim  >  Artikel  >  php教程  >  PHP 显示周日到周六 根据唐国伟代码改编

PHP 显示周日到周六 根据唐国伟代码改编

大家讲道理
大家讲道理Original
2016-11-08 11:39:021509Durchsuche

唐国伟发共享代码 显示周一到周日, 由于没有提供CSS设置,运行输出与截图不一致。可见,陪伴 PHP 的 CSS 同等重要! (见评论:  http://www.oschina.net/code/snippet_2318591_56527 )
首先解释一下 这里的 CSS 设置:
box{  
float:left;  ----- 如果页面还有横向空间的话,要求显示每一天数据的单元盒,都在同一行
margin-right:15px;  ---- 每个单元盒右边留出 15 像素 (px) 的空隙(间隔)
font-family:"楷体"; ---- 盒中汉字字体用楷体
}
#Today{ 
  color:#F00; ----显示当天的数据盒,用红色显示数据
  border-bottom:solid #00f 3px;   ------ 底边界用 兰色, 3 像素 (px) 粗的实线表示
}
主要改动:
1. 显示顺序改成:总是从周日到周六

2.首先设置好地方时区: 时区为亚洲上海,即中国地方时,这是必须的, 否则,由于可能的时差,有时会出现日期显示晚或早了一天

<html>
<header>
<meta charset="utf-8">
<style>
box{  
float:left;
margin-right:15px;
font-family:"楷体";
}
#Today{ 
    color:#F00;
    border-bottom:solid #00f 3px;
}
</style>
</header>
<body>
<?php
//设置好地方时区: 时区为亚洲上海,即中国地方时,这是必须的!
date_default_timezone_set(&#39;Asia/shanghai&#39;);
$week = date(&#39;w&#39;); //w-数字型的星期几,如:"0"(星期日)至"6"(星期六)
//创建中文星期几的数组
$week_cn=array(&#39;周日&#39;,&#39;周一&#39;,&#39;周二&#39;,&#39;周三&#39;,&#39;周四&#39;,&#39;周五&#39;,&#39;周六&#39;);
//调用方法 time() 获取当前地方时间的 Unix 时间戳(单位:秒)
//星期日的时间为 time()-$week*86400, 它是 循环体变量 $time 的初始值 
//每次循环结束,更新变量时,$time 增加一天的时间:86400秒
for($i=0,$time=time()-$week*86400; $i<7;$i++, $time+=86400){
    if ($i==$week) 
        echo &#39;<box id="Today"&#39;;
        else
        echo &#39;<box&#39;; 
    echo &#39;>&#39;.$week_cn[$i]; //输出中文的星期几
//输出对应的月份和号(日)
    echo &#39;<br>&#39;.date(&#39;m-d&#39;,$time).&#39;</box>&#39;; 
    }
?>
</body>
</html>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn