【问题标题】:Creating a calendar using PHP使用 PHP 创建日历
【发布时间】:2021-11-09 05:45:02
【问题描述】:

我是 php 新手,真的不知道我在做什么。我正在尝试使日历跨 4 行,宽 4 列。所以每行和每列一个月。例如,一月、二月、三月和四月将包含第 2 行列。我什至不知道如何开始。

Image 为例。

这是我目前所拥有的

<div class="row">
    <div class="column";">
        <h2>LOGO GOES HERE</h2>
        <p></p>
    </div>
    <div class="column";">
        <h2>2021</h2>
    </div>
    <div class="column">
        <h2>2021</h2>
    </div>
    <div class="column">
        <h2>2021</h2>
    </div>
</div>
<div class="row">
    <div class="column">
        <h2>January</h2>
    </div>
    <div class="column">

        <h2>February</h2>
    </div>
    <div class="column">
        <h2>March</h2>
    </div>
    <div class="column">
        <h2>April</h2>
    </div>
</div>
<div class="row">
    <div class="column">
        <h2>May</h2>
    </div>
    <div class="column">
        <h2>June</h2>
    </div>
    <div class="column">
        <h2>July</h2>
    </div>
    <div class="column">
        <h2>August</h2>
    </div>
</div>
<div class="row">
    <div class="column">
        <h2>September</h2>
    </div>
    <div class="column">
        <h2>October</h2> –

    </div>
    <div class="column">
        <h2>November</h2>
    </div>
    <div class="column">
        <h2>December</h2>
    </div>
</div>

【问题讨论】:

  • 你的意思是你想让一个页面显示 16 个月? (4 行 X 4 列,每个单元格一个月?)
  • 是的,我正在寻求帮助。
  • 这涉及多种语言,特别是 HTML、CSS 和 PHP。您是否需要跨越所有这些的东西,或者您有一个开始?在不混合 HTTP、特别是表单和可能的 JS 的情况下,这种特定的需求也很少见。需要这些吗?
  • 所以我确实有使用 HTML 和 CSS 的经验。我不知道从哪里开始。
  • 那么我会用 HTML 和 CSS 制作日历,然后合并 PHP 吗?如果是这样,我将如何合并它?

标签: php html css


【解决方案1】:

我认为这应该对您有所帮助:

  1. 选择您的开始日期 - 您可以使用 php 函数作为今天的日期

  2. 遍历 4 行,每行超过 4 列

  3. 显示随后每个月份的月份和年份

  4. 为每个月打开一个表和表头以显示天的标签

  5. 对于该月的每个星期,在相应的表格单元格中放入表格行和月份编号。 尝试在下面的代码中执行:

         <div style="width:100%; padding:0; margin:2%;"> 
         <?php
         for($j=0; $j<4; $j++) { 
         ?> <!--column of 4 rows -->
             <div style="width:22%; float:left; margin:1%; padding:0;"><!<!-- row of 4 months -->
                 <h4><?=date('F Y', strtotime($date));?></h4>
                 <table><!<!-- for each month -->
                     <thead><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th></thead>
                     <tbody>
                         <?php
                         for($m = date('m',strtotime($date)); 
                             $m == date('m', strtotime($date)); 
                             $date = date('Y-m-d',strtotime($date))) { ?>
                         <!-- for each week -->
                         <tr>
                             <?php if(date('w',strtotime($date))!=1) { ?>
                             <td colspan="<?=(date('w',strtotime($date))+6)%7;?>"></td> <?php } ?>
                             <?php
                             for($wkNo = date('W',strtotime($date)); 
                                 $wkNo == date('W',strtotime($date)); 
                                 $date = date('Y-m-d',strtotime($date)+$plus1day)) {
                                     if($m != date('m',strtotime($date))) { break;}
                                     ?>
                                 <!--for each day -->
                                 <td style="width:3%"><?=date('d',strtotime($date));?></td>
                          <?php } ?><!--end of week-->                  
                         </tr>   
                     <?php } ?>
                     </tbody>
                 </table>
             </div>
         <?php    } ?>
         </div>
     <?php } ?>
    

【讨论】:

    【解决方案2】:

    在纯 html 代码中,您可以使用表格代码构建。您可以使用 php 在 4 列上迭代 4 行,并且对于每个表,dayNames 为 thead,每周为 tr。

    <?php 
    for($i=0; $i<4; $i++) { 
     for($j=0; $j<4; $j++) { ?>
       <table><!-- for each month -->
         <thead>
           <th>MON</th><th>TUES</th><th>WED</th><th>THURS</th>
           <th>FRI</th><th>SAT</th><th>SUN</th>
         </thead>
         <tbody>
           <tr><!--iterate over weeks for month-->
             <td></td><!--iterate over days for the week-->
           </tr>
         </tbody>
       </table>
    <?php } } ?>
    

    【讨论】:

    • 这就是我所拥有的。

      一月

      二月

      三月

      四月

      五月

      六月

      七月

      八月

      9 月

      10 月

      11 月

      12 月

      Tu 我们 Th Fr Sa Su
    【解决方案3】:

    最简单的选择是使用内置的 HTML date 元素,当用户单击它时会添加一个日历视图。这是MDN Web Docs中提供的示例:

    <label for="start">Start date:</label>
    
    <input type="date" id="start" name="trip-start"
           value="2018-07-22"
           min="2018-01-01" max="2018-12-31">
    

    要使用 PHP 获取表单中的值,您需要执行以下操作。将其保存到名为 index.php 的文件中:

    <form action="get-date.php" method="POST">
    
      <label for="start">Start date:</label>
    
      <input type="date" id="start" name="trip-start"
           value="2018-07-22"
           min="2018-01-01" max="2018-12-31">
    
      <input type="submit" name="submit" value="submit">
    
    </form>
    

    现在在同一文件夹中创建一个名为 get-date.php 的文件并添加以下内容:

    <?php
    
      if(isset($_POST['submit'])) {
          $date = $_POST['trip-start'];
          echo $date;
      }
    
    ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多