<?php
class TimeDate{
private $year;
private $month;
private $start_week;
private $days;
public function __construct(){
$this->year = date("Y");
$this->month = date("m");
$this->start_week = date("w",mktime(0,0,0,$month,10,$year));
$this->days = date("t",mktime(0,0,0,$this->month,10,$this->year));
}
public function out(){
echo "<table align='center'>";
$this->week_list();
$this->day_list();
echo "</table>";
}
private function week_list(){
$week = array("日","一","二","三","四","五","六");
echo "<tr>";
for($i=0;$i<count($week);$i++){
echo "<th>".$week[$i]."</th>";
}
echo "</tr>";
}
private function day_list(){
echo "<tr>";
for($j=0;$j<$this->start_week;$j++){
echo "<td> </td>";
}
for($k=1;$k<=$this->days;$k++){
$j++;
if($k==date("d")){
echo "<td style='color:red;'>".$k."</td>";
}else{
echo "<td>".$k."</td>";
}
if($j%7==0){
echo "</tr><tr>";
}
}
echo "</tr>";
}
}
$td = new TimeDate();
$td->out();
?>构造方法中,一个使用$this,一个没有使用,按理说应该都用$this的,但是为什么,我只要给第一个加上this的话就不能达到我想要的效果。
另外“date("w")”里面的w是什么意思,谢谢。
class TimeDate{
private $year;
private $month;
private $start_week;
private $days;
public function __construct(){
$this->year = date("Y");
$this->month = date("m");
$this->start_week = date("w",mktime(0,0,0,$month,10,$year));
$this->days = date("t",mktime(0,0,0,$this->month,10,$this->year));
}
public function out(){
echo "<table align='center'>";
$this->week_list();
$this->day_list();
echo "</table>";
}
private function week_list(){
$week = array("日","一","二","三","四","五","六");
echo "<tr>";
for($i=0;$i<count($week);$i++){
echo "<th>".$week[$i]."</th>";
}
echo "</tr>";
}
private function day_list(){
echo "<tr>";
for($j=0;$j<$this->start_week;$j++){
echo "<td> </td>";
}
for($k=1;$k<=$this->days;$k++){
$j++;
if($k==date("d")){
echo "<td style='color:red;'>".$k."</td>";
}else{
echo "<td>".$k."</td>";
}
if($j%7==0){
echo "</tr><tr>";
}
}
echo "</tr>";
}
}
$td = new TimeDate();
$td->out();
?>构造方法中,一个使用$this,一个没有使用,按理说应该都用$this的,但是为什么,我只要给第一个加上this的话就不能达到我想要的效果。
另外“date("w")”里面的w是什么意思,谢谢。
(PHP 4, PHP 5)date — 格式化一个本地时间/日期说明
string date ( string $format [, int $timestamp ] )...m 数字表示的月份,有前导零 01 到 12
...
确实不是从外传入的,请原谅面向对象的小白可能你加入this的时候写错了(加入$this后面的$要去掉),你原来的代码会产生notice级错误这样写整个程序就就没错了$this->start_week = date("w",mktime(0,0,0,$this->month,10,$this->year)); 测试后才知道是个日历……-_-!
你可以测试一下,加上this跟没加this效果不一致,加上this是这样的:日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
不加this是这样的
日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30第二种是我要的效果,
写作 $this->start_week = date("w",mktime(0,0,0,$month,10,$year));
出现恰好正确的结果,纯属偶然 public function __construct(){
$this->year = date("Y");
$this->month = date("m");
$this->start_week = date("w",mktime(0,0,0,$month,10,$year));
$this->days = date("t",mktime(0,0,0,$this->month,10,$this->year));
}
应写作
public function __construct(){
$this->year = date("Y");
$this->month = date("m");
$this->start_week = date("w",mktime(0,0,0,$this->month,1,$year));
$this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
}
另外你这个类只能输出当前月的月历,更通用点的应写作
public function __construct($year='', $month=''){
$this->year = $year ? $year : date("Y");
$this->month = $month ? $month : date("m");
$this->start_week = date("w",mktime(0,0,0,$this->month,1,$year));
$this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
}或
public function __construct($month='', $year=''){
$this->year = $year ? $year : date("Y");
$this->month = $month ? $month : date("m");
$this->start_week = date("w",mktime(0,0,0,$this->month,1,$year));
$this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
}