PDA

View Full Version : calendario php


zebmckey
14-02-2018, 22:45
ho un problema con un piccolo calendario in php.
Praticamente mi restituisce tutti i mesi con 31 giorni. Come posso risolvere e assegnare ad ogni mese la giusta lunghezza?

calendario.inc


<?php
/**
* Calender class
*/

class Calender {

public $year = '';
public $month = '';
public $intToDay = array(
0=>'Sunday',
1=>'Moday',
2=>'Tuesday',
3=>'Wednesday',
4=>'Thursday' ,
5=>'Friday' ,
6=>'Saturday'
);
public $intToMonth = array(
1=>'January',
2=>'February',
3=>'March',
4=>'April' ,
5=>'May' ,
6=>'June' ,
7=>'July',
8=>'August',
9=>'September',
10=>'October',
11=>'November' ,
12=>'December'
);

/**
* constructor function
*/
public function Calender($year) {

$this->year = $year;
}

/**
* get code for
*
* @param int $month
*
*/
public function getCodeForMonth($month) {

$this->month = $month;

$startingDay = date("l", mktime(0, 0, 0, $month, 1, $this->year));

$returnCode = '<table border="1" align="center" style="color:brown"> <tr>';
for ($count = 0; $count < 7; $count++) {
$returnCode .= "<td align=\"center\" style=\"color:red\"><strong>".$this->intToDay[$count]."</strong></td>";
}
$returnCode .= "</tr> <tr>";

for ($count = 0; $count < array_search($startingDay, $this->intToDay); $count++) {
$returnCode .= "<td><br /></td>";
}

for ($counter = 1; $counter <= 31; $counter++, $count++) {
if (($count % 7) == 0) {
$returnCode .= "</tr> <tr>";
}
$returnCode .= "<td align=\"center\">".$counter."</td>";
}
$returnCode .= '</table>';

return $returnCode;
}

public function getCode($month) {

$this->month = $month;

$startingDay = date("l", mktime(0, 0, 0, $month, 1, $this->year));

$returnCode = '<table border="1" align="center" style="color:brown"> <tr>';
for ($count = 0; $count < 7; $count++) {
$returnCode .= "<td width=\"65\" align=\"center\" style=\"color:#000000;background-color:#6779AC\"><strong>".$this->intToDay[$count]."</strong></td>";
}
$returnCode .= "</tr> <tr>";

for ($count = 0; $count < array_search($startingDay, $this->intToDay); $count++) {
$returnCode .= "<td><br /></td>";
}

for ($counter = 1; $counter <= 31; $counter++, $count++) {
if (($count % 7) == 0) {
$returnCode .= "</tr> <tr>";
}
$monthName = $this->intToMonth[$month];

if ( ($month < date('n') || $this->year < date('Y')) )
{
$returnCode .= "<td align=\"center\" style=\"background-color:#ABCED6;\"><span><b>".trim($counter)."</b></span></td>";
}
else {
$returnCode .= "<td align=\"center\" style=\"background-color:#ABCED6;\"><span style=\"cursor:pointer;cursor:hand;background-color:#ABCED6;\" onclick=\"openWindow('$counter-$this->month-$this->year');\"><b>".trim($counter)."</b></span></td>";
}
}
$returnCode .= '</table>';

return $returnCode;
}

}
?>


test.php


<?php

require_once('calender.inc.php');

$calender = new Calender('2018');

for ($i = 0; $i < 12; $i++) {
$month = $i+1;
echo "<center><hr />".$calender->intToMonth[$month]." ".$calender->year."<hr /></center>".$calender->getCodeForMonth($i);
}

?>
<!--table align="center" width="80%" align="center" border="1">
<tr>
<td align="center" style="font-family:verdana;font-size:30">
<strong> <?php echo $calender->intToMonth[$calender->month].' '.$calender->year; ?></strong>
</td>
</tr>

<tr>
<td>
<?php echo $jan; ?>
</td>
</tr>
</table -->

Grazie per il vostro prezioso aiuto

RagingBull
15-02-2018, 10:02
potresti inserire un controllo sul mese che vuoi stampare: se hai aprile, giugno, settembre e novembre il tuo loop si fermerà a 30; se hai febbraio puoi fare un controllo veloce sull'anno per vedere se è bisestile e quindi impostare il loop a 28 o a 29; in tutti gli altri casi 31.

Pbdz
18-02-2018, 18:41
Puoi usare la funzione cal_days_in_month() che ti restituisce il numero di giorni in un determinato mese.

Qui un esempio di come usarla:
https://www.w3schools.com/php/func_cal_cal_days_in_month.asp

zebmckey
19-02-2018, 15:44
potresti inserire un controllo sul mese che vuoi stampare: se hai aprile, giugno, settembre e novembre il tuo loop si fermerà a 30; se hai febbraio puoi fare un controllo veloce sull'anno per vedere se è bisestile e quindi impostare il loop a 28 o a 29; in tutti gli altri casi 31.Scusami, io sono un neofita e sto imparando, come lo inserisco un controllo? Devo richiamare l'array e far sì che il $counter fermi il loop a 30?

Inviato dal mio PLK-L01 utilizzando Tapatalk