3

I have created a php calender which will show one week at a time.Here is the code i have created

 <?php
   $week = date("W");
   $year = (isset($_GET['year']))?$_GET['year']:date("Y");
   $week = (isset($_GET['week']))?$_GET['week']:Date('W');
   if($week>53){
     $year+= 1;
     $week=1;
   }
 ?>

 <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week+1).'&year='.$year; ?>">Next  Week</a> <!--Next week-->

 <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week-1).'&year='.$year; ?>">Pre Week</a> <!--Previous week-->

 <table border="1px">
 <tr>
 <td>Employee</td>
 <?php
 for($day=1; $day<=7; $day++)
 {
    $d = strtotime($year."W".$week.$day);                           
    echo "<td>".date('l',$d )."<br>";
    echo date('d M',$d)."</td>";
 }
 ?>
 </tr>

when i am trying to go to the next week it is working correctly. But when the year is changing it is not working for the next year.

4

2 回答 2

15

Leave the week calculation to the DateTime::setIsoDate() method.

Here is the simplest and best solution for your problem :

<?php
$dt = new DateTime;
if (isset($_GET['year']) && isset($_GET['week'])) {
    $dt->setISODate($_GET['year'], $_GET['week']);
} else {
    $dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>

<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week-1).'&year='.$year; ?>">Pre Week</a> <!--Previous week-->
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week+1).'&year='.$year; ?>">Next Week</a> <!--Next week-->

<table>
    <tr>
        <td>Employee</td>
<?php
do {
    echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "</td>\n";
    $dt->modify('+1 day');
} while ($week == $dt->format('W'));
?>
    </tr>
</table>
于 2013-09-21T19:00:33.510 回答
2

When you're using strtotime, your week has to be two-digit. You have to prepend a zero, if the week is lower than 10 before the for loop.

if($week < 10) {
    $week = '0'. $week;
}
for($day = 1; $day <= 7; $day++) {

Also, a year only has 52 weeks, the condition at the beginning should be.

if($week > 52) {
    $year++;
    $week = 1;
} elseif($week < 1) { // If you want the possibility to go back too
    $year--;
    $week = 52;
}

Full code:

<?php
$year = (isset($_GET['year'])) ? $_GET['year'] : date("Y");
$week = (isset($_GET['week'])) ? $_GET['week'] : date('W');
if($week > 52) {
    $year++;
    $week = 1;
} elseif($week < 1) {
    $year--;
    $week = 52;
}
?>

<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next  Week</a> <!--Next week-->
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 1 ? 52 : $week -1).'&year='.($week == 1 ? $year - 1 : $year); ?>">Pre Week</a> <!--Previous week-->

<table border="1px">
    <tr>
        <td>Employee</td>
<?php
if($week < 10) {
    $week = '0'. $week;
}
for($day= 1; $day <= 7; $day++) {
    $d = strtotime($year ."W". $week . $day);

    echo "<td>". date('l', $d) ."<br>". date('d M', $d) ."</td>";
}
?>
    </tr>
</table>
于 2013-09-18T09:25:56.820 回答