PHP 달력 소스 - PHP dallyeog soseu

PHP 달력 소스 - PHP dallyeog soseu

<?php

print "<h2>" .date("F"). "</h2>";

print "Today:" .date("y/m/d"). "  ".date(l);

print "<br>";

print "<br>";

$arr_day = array("월","화","수","목","금","토","일");

print "<table border=1><tr>";

foreach($arr_day as $value){

print "<th>" .$value. "</th>";

}

print "</tr><tr>";

for($i=0;$i<date("t");$i++){

$week_num = date("w",mktime(0,0,0,date("n"),$i+1, date("Y")));

if($i==0){

print "<tr>";

space($week_num);

}

elseif($week_num == 0){

print "<tr>";

}

if($week_num == 5){

print "<Td><font color = blue>".($i+1)."</font></td>";

}elseif($week_num == 6){

print "<Td><font color = red>".($i+1)."</font></td>";

}else{

print "<Td>".($i+1)."</td>";

}

if($i+1 == date("t")){

space(6-$week_num);

print "</tr>";

}

elseif($week_num == 6){

print "</tr>";

}

}

print "</table>";

function space($count){

if($count !=0){

for($i=0;$i<$count;$i++){

print "<td></td>";

}

}

}

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

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

<?php

classPN_Calendar{

    public$first_day_of_week=0;//0 - sunday, 1 - monday

    public$current_year=null;

    public$current_month=null;

    public$current_day=null;

    public$show_year_selector=true;

    public $min_select_year=1991;

    public$max_select_year=2045;

    public$show_month_selector=true;

    public function__construct($atts=array()){

        if(isset($atts['first_day_of_week'])){

            $this->first_day_of_week =$atts['first_day_of_week'];

        }

        if(!isset($atts['year'])){

            $this->current_year =date('Y');

        }else{

            $this->current_year=$atts['year'];

        }

        if(!isset($atts['month'])){

            $this->current_month =date('m');

        }else{

            $this->current_month=$atts['month'];

        }

        if(!isset($atts['day'])){

            $this->current_day =date('d');

        }else{

            $this->current_day=$atts['day'];

        }

        //***

        if(isset($atts['show_year_selector'])){

            $this->show_year_selector =$atts['show_year_selector'];

        }

        if(isset($atts['show_month_selector'])){

            $this->show_month_selector =$atts['show_month_selector'];

        }

        if(isset($atts['min_select_year'])){

            $this->min_select_year =$atts['min_select_year'];

        }

        if(isset($atts['max_select_year'])){

            $this->max_select_year =$atts['max_select_year'];

        }

    }

    /*

     * Month calendar drawing

     */

    public functiondraw($data=array(),$y=0,$m=0){

        //***

        if ($m==0AND$m==0){

            $y=$this->current_year;

            $m =$this->current_month;

        }

        //***

        $data['week_days_names']=$this->get_week_days_names(true);

        $data['cells']=$this->generate_calendar_cells($y,$m);

        $data['month_name']= $this->get_month_name($m);

        $data['year']=$y;

        $data['month']=$m;

        $data['events']=array();//here you can transmit events from database (look PN_CalendarCell::draw($events))

        return$this->draw_html('calendar', $data);

    }

    privatefunctiongenerate_calendar_cells($y,$m){

        $y= intval($y);

        $m=intval($m);

        //***

        $first_week_day_in_month= date('w',mktime(0,0,0,$m,1,$y));//from 0 (sunday) to 6 (saturday)

        $days_count =$this->get_days_count_in_month($y,$m);

        $cells=array();

        //***

        if ($this->first_day_of_week==$first_week_day_in_month){

            for($d=1;$d<= $days_count;$d++){

                $cells[]=newPN_CalendarCell($y,$m,$d);

            }

            //***

            $cal_cells_left=5*7-$days_count;

            $next_month_data =$this->get_next_month($y,$m);

            for($d=1;$d<=$cal_cells_left; $d++){

                $cells[]=newPN_CalendarCell($next_month_data['y'], $next_month_data['m'],$d,false);

            }

        }else{

            //***

            if($this->first_day_of_week==0){

                $cal_cells_prev=6- (7-$first_week_day_in_month);//checked, is right

            }else{

                if ($first_week_day_in_month==1){

                    $cal_cells_prev=0;

                } else{

                    if($first_week_day_in_month==0){

                        $cal_cells_prev =6-1;

                    }else{

                        $cal_cells_prev =6-(7-$first_week_day_in_month)-1;

                    }

                }

            }

            //***

            $prev_month_data=$this->get_prev_month($y, $m);

            $prev_month_days_count=$this->get_days_count_in_month($prev_month_data['y'],$prev_month_data['m']);

            for ($d=$prev_month_days_count-$cal_cells_prev;$d<=$prev_month_days_count;$d++){

                $cells[] =newPN_CalendarCell($prev_month_data['y'],$prev_month_data['m'],$d,false);

            }

            //***

            for($d=1;$d<=$days_count;$d++){

                $cells[] =newPN_CalendarCell($y,$m,$d);

            }

            //***

            //35(7*5) or 42(7*6) cells

            $busy_cells=$cal_cells_prev+$days_count;

            $cal_cells_left=0;

            if ($busy_cells<35){

                $cal_cells_left=35-$busy_cells-1;

            } else{

                $cal_cells_left=42-$busy_cells-1;

            }

            //***

            if($cal_cells_left>0){

                $next_month_data=$this->get_next_month($y, $m);

                for($d=1;$d<=$cal_cells_left;$d++){

                    $cells[] =newPN_CalendarCell($next_month_data['y'],$next_month_data['m'],$d,false);

                }

            }

        }

        //***

        return$cells;

    }

    public functionget_next_month($y,$m){

        $y=intval($y);

        $m= intval($m);

        //***

        $m++;

        if($m%13==0 OR$m>12){

            $y++;

            $m=1;

        }

        returnarray('y'=>$y,'m'=>$m);

    }

    publicfunction get_prev_month($y,$m){

        $y=intval($y);

        $m= intval($m);

        //***

        $m--;

        if($m<=0){

            $y--;

            $m=12;

        }

        returnarray('y'=>$y,'m' =>$m);

    }

    publicfunctionget_days_count_in_month($year,$month){

        return $month==2?($year%4?28:($year%100?29: ($year%400?28:29))):(($month-1)%7% 2?30:31);

    }

    publicstaticfunctionget_month_name($m){

        $names =self::get_monthes_names();

        return$names[intval($m)];

    }

    public functionget_week_day_name($num,$shortly=false){

        $names=$this->get_week_days_names($shortly);

        return $names[intval($num)];

    }

    publicfunctionget_week_days_names($shortly=false){

        if ($this->first_day_of_week==1){

            if($shortly){

                return array(

                    1=>'Mo',

                    2=>'Tu',

                    3=>'We',

                    4=>'Th',

                    5 =>'Fr',

                    6=>'Sa',

                    7=> 'Su'

                );

            }

            returnarray(

                1 =>'Monday',

                2=>'Tuesday',

                3=>'Wednesday',

                4=>'Thursday',

                5=>'Friday',

                6 =>'Saturday',

                7=>'Sunday'

            );

        } else{

            if($shortly){

                returnarray(

                    0 =>'Su',

                    1=>'Mo',

                    2=> 'Tu',

                    3=>'We',

                    4=>'Th',

                    5=>'Fr',

                    6=>'Sa'

                );

            }

            returnarray(

                0=>'Sunday',

                1 =>'Monday',

                2=>'Tuesday',

                3=>'Wednesday',

                4=>'Thursday',

                5=>'Friday',

                6 =>'Saturday'

            );

        }

    }

    publicstaticfunction get_monthes_names(){

        returnarray(

            1=>'January',

            2 =>'February',

            3=>'March',

            4=>'April',

            5 =>'May',

            6=>'June',

            7=>'July',

            8 =>'August',

            9=>'September',

            10=>'October',

            11 =>'November',

            12=>'December'

        );

    }

    public functiondraw_html($view,$data=array()){

        @extract($data);

        ob_start();

        include('views/'.$view.'.php');

        returnob_get_clean();

    }

}

//---------------------------------------------------------------------------------------

classPN_CalendarCell{

    public$cell_year=null;

    public$cell_month= null;

    public$cell_day=null;

    public$in_current_month=true;

    publicfunction __construct($y,$m,$d,$in_current_month=true){

        $this->cell_year=$y;

        $this->cell_month =$m;

        $this->cell_day=$d;

        $this->in_current_month=$in_current_month;

    }

    public functionget_week_day_num(){

        returndate('w',mktime(0,0,0,$this->cell_month, $this->cell_day,$this->cell_year));//from 0 (sunday) to 6 (saturday);

    }

    publicfunctiondraw($events) {

        $this_day_events=0;

        if(is_array($events)){

            if (isset($events[$this->cell_year][$this->cell_month][$this->cell_day])){

                $this_day_events =count($events[$this->cell_year][$this->cell_month][$this->cell_day]);

            }

        } else{

            $events=array();

        }

        ?>

        <span class="pn_cal_cell_ev_counter"<?phpif($this_day_events<=0):?>style="display: none;"<?phpendif;?>><?php echo$this_day_events?></span>

        <adata-year="<?phpecho$this->cell_year?>" data-month="<?phpecho$this->cell_month?>"data-day="<?phpecho$this->cell_day?>" data-week-day-num="<?phpecho$this->get_week_day_num()?>"href="javascript:void(0);"class="<?phpif($this->in_current_month): ?>pn_this_month<?phpelse:?>other_month<?phpendif;?>"><?phpecho$this->cell_day?></a>

        <?php

    }

}