【问题标题】:PHP calendar showing dates on the right day of the weekPHP 日历显示一周中正确日期的日期
【发布时间】:2016-10-15 17:41:26
【问题描述】:

我在我的 MySQL 数据库中创建了一个表,其中包含一整月的日期。

然后我尝试将它们显示在表格中(看起来像日历)

我创建了一个脚本,因此每月的第一天显示在正确的日期(例如 10 月的星期六)。

然后我会在它之后显示所有其他日期。

代码

$Firstdate = date('Y-m-01');
       $FirstDay = date("N", strtotime($Firstdate));



if ($FirstDay == 1) {}
if ($FirstDay == 2) { echo"<td></td>";}
if ($FirstDay == 3) { echo"<td></td> <td></td>";}
if ($FirstDay == 4) { echo"<td></td> <td></td> <td></td>";}
if ($FirstDay == 5) { echo"<td></td> <td></td> <td></td> <td></td>";}
if ($FirstDay == 6) { echo"<td></td> <td></td> <td></td> <td></td> <td></td>";}
if ($FirstDay == 7) { echo"<td></td> <td></td> <td></td> <td></td> <td></td> <td></td>";}

 $result=mysql_query("SELECT * FROM calendar")or die('ERROR 315' );
 $num_rows = mysql_num_rows($result);
  for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
    $row = mysql_fetch_array($result);

    $TheDate = $row ['date'];
   $TheDateF = date("jS", strtotime($TheDate));

echo " <td> &nbsp;&nbsp;$TheDateF</Center><br><br><br><br></td>";   

   if ($i % 7 == 0) {
              echo '</tr><tr>'; // it's time no move to next row
            }
  }

这从正确的日期开始,但是请参见下面的屏幕截图,我需要它在星期日日期之后开始新的一行:

有什么办法可以解决这个问题吗?

【问题讨论】:

  • 有什么理由让你硬编码日历,因为有很多可用的插件。
  • 应我的客户要求...
  • @Suraj - 你有什么想法吗?
  • 我只会使用像 FullCalendar (fullcalendar.io) 这样的插件。还有其他几个。它将为您节省大量时间和问题,并为您的客户节省大量资金。然后你就可以继续为他们提供一些真正的价值,而不是重新发明一个(相对复杂的)轮子
  • @ADyson 谢谢,但我非常接近拥有一个没有插件的定制产品,这里就是这个问题。

标签: php mysql html-table


【解决方案1】:

试试这个代码。根据您的需要进行修改。我很确定这会对您有所帮助。

<?php
/* Set the default timezone */
date_default_timezone_set("America/Montreal");

/* Set the date */
$date = strtotime(date("Y-m-d"));

$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = array();
for ($i = 0; $i < 7; $i++) {
  $weekDays[] = strftime('%a', $timestamp);
  $timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>
<table class='table table-bordered' border=1 style="table-layout: fixed;">
  <tr>
    <th colspan="7" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
  </tr>
  <tr>
    <?php foreach($weekDays as $key => $weekDay) : ?>
      <td class="text-center"><?php echo $weekDay ?></td>
    <?php endforeach ?>
  </tr>
  <tr>
    <?php for($i = 0; $i < $blank; $i++): ?>
      <td></td>
    <?php endfor; ?>
    <?php for($i = 1; $i <= $daysInMonth; $i++): ?>
      <?php if($day == $i): ?>
        <td><strong><?php echo $i ?></strong></td>
      <?php else: ?>
        <td><?php echo $i ?></td>
      <?php endif; ?>
      <?php if(($i + $blank) % 7 == 0): ?>
        </tr><tr>
      <?php endif; ?>
    <?php endfor; ?>
    <?php for($i = 0; ($i + $blank + $daysInMonth) % 7 != 0; $i++): ?>
      <td></td>
    <?php endfor; ?>
  </tr>
</table>

来源 http://code.runnable.com/VKxpI5dzCMkTrRq1/simple-calendar-for-php

【讨论】:

  • 太棒了!谢谢。如何将项目添加到一天? (我在代码的什么地方添加这个?)
猜你喜欢
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多