PHP 日历在一周中的正确日期显示日期

标签 php mysql html-table

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

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

我创建了一个脚本,所以该月的第一天显示在正确的日期(例如,星期六表示十月)。

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

代码

$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
            }
  }

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

enter image description here

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

最佳答案

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

<?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

关于PHP 日历在一周中的正确日期显示日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002057/

相关文章:

php - 如何使用PHP MYSQL使用的sql查询通过添加自己的行来选择记录

mysql - 如何将数据从一个 SQLite 移动到具有不同设计的 MySQL?

php - 如何在mysql数据库中插入像 "Pyrénées, France'”这样的字符串?

javascript - 如何使用 jQuery 将两个 select 元素附加到表行?

javascript - 将我的 html 破折号菜单转换为 php

javascript - 使用 PHP 更新产品详细信息的 Shopify Api

php - SQL:基于条件的字段值?

mysql - 想要解析 MySQL 中的 XML 文件(使用预定义的函数/过程)并将其插入到表中

jquery - 如何使用 jQuery 过滤表格?

html - 如何更改 td 标签内的元素位置