Jump to content
  • 0

Календарь на php


vvsh
 Share

Question

Здрасте, мне нужно сделать календарь вот такой:

http://floomby.ru/content/w7HGP3KuJE/

Может меня кто натолкнет на мысль, как именно можно сделать, чтобы он выводил правильную структуру месяца?

заранее спасибо

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Когда-то давно я сделал так:

date("w",mktime(0,0,0,date("n"),1,date("Y"))); - первое число месяца

date("t") - количество дней в неделе.

Ну и потом заполняем пустые клетки for ($i=1;$i<date("w",mktime(0,0,0,date("n"),1,date("Y")));$i++)

и потом ставим циферьки. Что-то типтого. Всё элементарно вроде...

Link to comment
Share on other sites

  • 0
Когда-то давно я сделал так:

date("w",mktime(0,0,0,date("n"),1,date("Y"))); - первое число месяца

date("t") - количество дней в неделе.

Ну и потом заполняем пустые клетки for ($i=1;$i<date("w",mktime(0,0,0,date("n"),1,date("Y")));$i++)

и потом ставим циферьки. Что-то типтого. Всё элементарно вроде...

первое число месяца неважно абсолютно :) оно всегда 1,

важно:

- сколько дней в месяце

- с какого дня начинается неделя

дальше простой цикл и пара условий

ни у какой сегодня день конечно :)

Link to comment
Share on other sites

  • 0

date("w",mktime(0,0,0,date("n"),1,date("Y"))) как раз и вернёт нам день недели первого числа текущего месяца

date("t"), конечно, количество дней в месяце - это я просто сделал в слове "месяце" 6 ошибок

Link to comment
Share on other sites

  • 0

Посмотрите ...

<html>
<head>
<title> Calendar </title>
</head>

<style>
/* CALENDAR */
table.cal { border-collapse: collapse; border: 1px solid #eaeaea; }
table.cal td { text-align: center; }
td.center { font-weight: bold; color: #216792; }
td.prev { width: 25px; text-align: center; }
td.prev a { font-wegith: bold; }
td.center { text-align: center; width: 100%; }
td.next { width: 25px; text-align: center; }
td.next a { font-wegith: bold; }
table.cal td { padding: 3px; }
/* current date */
table.cal td.cdt { background: #f0f4fe; font-weight: bold; }
/* current date is sunday */
table.cal td.scd { background: #E83903; color: #fff; }
/* default days */
table.cal td.def { color: #5b6376; }
/* sunday days */
table.cal td.sbg { color: #dc143c; background: #f7f7f7; }
/* week defaults */
table.cal tr.week td { width: 20px; text-align: center; }
/* week default sunday day */
table.cal td.wsd { background: #000064; color: #ffffff;}
/* week default days */
table.cal td.wdd { background: #e0eaff; color: 4974d5; }
/* week selected day */
table.cal td.wsl { background: #e0eaff; color: #000064;}
table.cal td.wss { background: #ffa500; color: #ffffff;}

</style>

<body>
<?php

class Calendar {

var $year;
var $month;
var $date;
var $leap;
var $lang;
var $path;
var $dates;

function Calendar($l)
{
$this->year = date('Y');
$this->month = date('n');
$this->date = date('j');
$this->leap = date('L');
$this->lang = $l;
$this->month_names = array(
'uz'=>array(1=>'Yanvar', 'Fevral', 'Mart', 'Aprel', 'May', 'Iyun', 'Iyul', 'August', 'Sentyabr', 'Oktyabr', 'Noyabr', 'Dekabr'),
'ru'=>array(1=>'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'),
'en'=>array(1=>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'Septempber', 'Oktober', 'November', 'December')
);
$this->week_names = array(
'uz'=>array('Du', 'Se', 'Ch', 'Pa', 'Ju', 'Sh', 'Ya'),
'ru'=>array('Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'),
'en'=>array('Su', 'Mo', 'Tu', 'We', 'Tr', 'Fr', 'St')
);

$this->dates = array();
}

function Days(){
if($this->month==3 or $this->month==6 or $this->month==9 or $this->month==11){
return 30;
}else if($this->month==2){
if($this->leap){
return 29;
}else{
return 28;
}
}else{
return 31;
}
}

function First(){
$first_day = date('w', mktime(0, 0, 0, $this->month, 1, $this->year));
if($this->lang=='uz' or $this->lang=='ru'){
$first_day = ($first_day==0) ? 6 : $first_day - 1;
}
return $first_day;
}

function Week(){
return date('w', mktime(0, 0, 0, $this->month, date('d'), $this->year));
}

function Body()
{
$count_days = 0;
$space_days = 0;

$tbl = "<table cellspacing=0 cellpadding=0 border=0 class='cal'>";
$tbl.= "<tr>";
$tbl.= "<td colspan=7 align=center>";
$tbl.= "<table cellspacing=0 cellpadding=0 border=0 style='border-collapse:collapse'>";
$tbl.= "<tr>";
if(($this->month-1)==0){
$tbl.= "<td class='prev'><a href='".$this->path."y=".($this->year-1)."&m=12&d=".$this->date."'>«</a></td>";
}else{
$tbl.= "<td class='prev'><a href='".$this->path."y={$this->year}&m=".($this->month-1)."&d=".$this->date."&a=p'>«</a></td>";
}
$tbl.= "<td class='center'>".$this->month_names[$this->lang][$this->month]." ".$this->year."</td>";
if(($this->month+1)==13){
$tbl.= "<td class='next'><a href='".$this->path."y=".($this->year+1)."&m=1&d=".$this->date."&a=n'>»</a></td>";
}else{
$tbl.= "<td class='next'><a href='".$this->path."y={$this->year}&m=".($this->month+1)."&d=".$this->date."'>»</a></td>";
}
$tbl.= "</tr>";
$tbl.= "</table>";
$tbl.= "</td>";
$tbl.= "</tr>";
$tbl.= "<tr class='week'>";
for($col=0; $col<7; $col++){
$w = $this->Week();
$val = $this->week_names[$this->lang][$col];
if($this->lang=='uz' or $this->lang=='ru'){
if($w==0 && $col==6){
$tbl.= "<td class='wss'>{$val}</td>";
}else{
if($col==($w-1)){
$tbl.= "<td class='wsl'>{$val}</td>";
}else{
$cls = ($col==6) ? 'wsd' : 'wdd';
$tbl.= "<td class='$cls'>{$val}</td>";
}
}
}else{
if($col==$w){
$cls = ($col==0) ? 'wss' : 'wsl';
$tbl.= "<td class='{$cls}'>{$val}</td>";
}else{
$cls = ($col==0) ? 'wsd' : 'wdd';
$tbl.= "<td class='{$cls}'>{$val}</td>";
}
}
}
$tbl.= "</tr>";
for($row=0; $row<6; $row++){
$tbl.= "<tr>";
for($col=0; $col<7; $col++){
if($space_days<$this->First()){
$tbl.= "<td> </td>";
}else{
$count_days++;
if($count_days<=$this->Days()){
$en = ($this->lang=='en') ? 0 : 6;
if($count_days==date('d')){
$cls = ($en==$col) ? 'scd' : 'cdt';
$tbl.= "<td class='{$cls}'>";
if($this->getDates($count_days)){
$m = $this->month;
$d = $count_days;
$tbl.= "<a href='".$this->path."y={$this->year}&m={$m}&d={$d}'><b>{$count_days}</b></a>";
}else{
$tbl.= $count_days;
}
$tbl.= "</td>";
}else{
$cls = ($en==$col) ? 'sbg' : 'def';
$tbl.= "<td class='{$cls}'>";
if($this->getDates($count_days)){
$m = $this->month;
$d = $count_days;
$tbl.= "<a href='".$this->path."y={$this->year}&m={$m}&d={$d}'><b>{$count_days}</b></a>";
}else{
$tbl.= $count_days;
}
$tbl.= "</td>";
}
}else{
$tbl.= "<td> </td>";
}
}
$space_days++;
}
$tbl.= "</tr>";
}
$tbl.= "</table>";

return $tbl;
}

function setAction()
{
if(!empty($_GET['y']) && !empty($_GET['m']) && !empty($_GET['d']))
{
$y = (int) $_GET['y'];
$m = (int) $_GET['m'];
$d = (int) $_GET['d'];

if(is_numeric($y) && is_numeric($m) && is_numeric($d)){
if($y>=2000 && $y<=2050){ $this->year = $y; }
if($m<=12){ $this->month = $m; }
if($d<=$this->Days()){ $this->date = $d; }
}
}
}

function getDates($d)
{
$result = 0;
if(is_array($this->dates) && count($this->dates)>0){
foreach($this->dates as $k=>$v){
if(!preg_match("/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$/i", $v)){
die("Incorrect date: {$v}");
}else{
$m = ($this->month<10) ? '0'.$this->month : $this->month;
$c = ($d<10) ? '0'.$d : $d;
$y = $this->year;
$l = trim($y.'-'.$m.'-'.$c);
if(strcmp($v, $l)==0){ $result = 1; }
}
}
}
return $result;
}
}

$cal = new Calendar('ru'); // ru | en

$cal->setAction();

$cal->path = 'calendar.php?';

$cal->dates = array('2009-08-21');

print $cal->Body();

?>

</body>
</html>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy