1
I have a calendar in php and would like to know two things !
How to make the date start Monday ? (Monday 1, for example) and how to make Sundays appear in red.
Here’s the code:
<!DOCTYPE HTML>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Calendário em PHP</title>
<?php
date_default_timezone_set('America/Sao_Paulo');
?>
</head>
<body>
<h1>Estamos em <?php echo date('Y');?></h1>
<p>Hoje é dia <strong><?php echo date('d / '); ?></strong>
<?php echo date('m'); ?>
agora são <?php echo date ('H'); ?>horas e
<?php echo date('i');?> minutos.</p>
<?php
function linha($semana){
echo "<tr>";
for ($i = 0; $i <=6; $i++){
if(isset($semana[$i])){
echo "<td>{$semana[$i]}</td>";
} else{
echo "<td></td>";
}
}
echo "</tr>";
}
function calendario(){
$dia = 1;
$semana = array();
while($dia <= 31){
array_push($semana, $dia);
if(count($semana) == 7){
linha($semana);
$semana = array();
}
$dia++;
}
linha($semana);
}
?>
<table border="1">
<tr>
<th>Dom</th>
<th>Seg</th>
<th>Ter</th>
<th>Qua</th>
<th>Qui</th>
<th>Sex</th>
<th>Sáb</th>
<?php calendario(); ?>
</tr>
</table>
</body>
</html>
For parts, to paint Sundays red is simple, in its line function vc creates an if by checking if the number is the corresponding Sunday, if it is vc puts a tag class in its tr(ex: <tr class="red"> and stylizes with CSS(ex: . red{ color: red;}). Already to count from second there are several ways, we know that the date() command can return numbers that represent the days of the week (Sunday = 1, Monday =2 and etc), just that in its array of days you use the for from 1 to n-1 and put the index 0 last, after the execution of the is, which in this case represents Sunday.
– Raul Sena Ferreira