【问题标题】:Where does href link points to in html code that uses also php (php calendar)?在也使用 php(php 日历)的 html 代码中,href 链接指向哪里?
【发布时间】:2020-11-11 04:51:57
【问题描述】:

我正在尝试采用一个 html 代码来绘制一个没有事件的日历到一个在日历上显示事件的代码。我知道代码构建了 $weeks 数组,该数组稍后在用于绘制表格的代码中。如果在屏幕上按下 ,日历会前后切换月份。所以我的想法是一旦按下 就会重建 $weeks 数组,并且代码会重新绘制日历。 代码中a-tag中的href在哪里指向一次被按下。按下 ) 后代码是否会跳转到 $prev ($next) 行?代码来自以下网站https://codingwithsara.com/how-to-code-calendar-in-php/#Video。我还在下面发布了代码。

我想在该日历上显示我的事件是读取我的数据库提取事件,然后使用年、月和日期循环遍历它们以正确显示在日历的当前页面上。

谢谢。

阿金

<?php
// Set your timezone
date_default_timezone_set('Asia/Tokyo');

// Get prev & next month
if (isset($_GET['ym'])) {
    $ym = $_GET['ym'];
} else {
    // This month
    $ym = date('Y-m');
}

// Check format
$timestamp = strtotime($ym . '-01');
if ($timestamp === false) {
    $ym = date('Y-m');
    $timestamp = strtotime($ym . '-01');
}

// Today
$today = date('Y-m-j', time());

// For H3 title
$html_title = date('Y / m', $timestamp);

// Create prev & next month link     mktime(hour,minute,second,month,day,year)
$prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp)));
$next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));
// You can also use strtotime!
// $prev = date('Y-m', strtotime('-1 month', $timestamp));
// $next = date('Y-m', strtotime('+1 month', $timestamp));

// Number of days in the month
$day_count = date('t', $timestamp);
 
// 0:Sun 1:Mon 2:Tue ...
$str = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
//$str = date('w', $timestamp);


// Create Calendar!!
$weeks = array();
$week = '';

// Add empty cell
$week .= str_repeat('<td></td>', $str);

for ( $day = 1; $day <= $day_count; $day++, $str++) {
     
    $date = $ym . '-' . $day;
     
    if ($today == $date) {
        $week .= '<td class="today">' . $day;
    } else {
        $week .= '<td>' . $day;
    }
    $week .= '</td>';
     
    // End of the week OR End of the month
    if ($str % 7 == 6 || $day == $day_count) {

        if ($day == $day_count) {
            // Add empty cell
            $week .= str_repeat('<td></td>', 6 - ($str % 7));
        }

        $weeks[] = '<tr>' . $week . '</tr>';

        // Prepare for new week
        $week = '';
    }

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>PHP Calendar</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
    <style>
        .container {
            font-family: 'Noto Sans', sans-serif;
            margin-top: 80px;
        }
        h3 {
            margin-bottom: 30px;
        }
        th {
            height: 30px;
            text-align: center;
        }
        td {
            height: 100px;
        }
        .today {
            background: orange;
        }
        th:nth-of-type(1), td:nth-of-type(1) {
            color: red;
        }
        th:nth-of-type(7), td:nth-of-type(7) {
            color: blue;
        }
    </style>
</head>
<body>
    <div class="container">
        <h3><a href="?ym=<?php echo $prev; ?>">&lt;</a> <?php echo $html_title; ?> <a href="?ym=<?php echo $next; ?>">&gt;</a></h3>
        <table class="table table-bordered">
            <tr>
                <th>S</th>
                <th>M</th>
                <th>T</th>
                <th>W</th>
                <th>T</th>
                <th>F</th>
                <th>S</th>
            </tr>
            <?php
                foreach ($weeks as $week) {
                    echo $week;
                }
            ?>
        </table>
    </div>
</body>
</html>

【问题讨论】:

  • 不确定我是否理解您的问题。如果你只想知道&lt;h3&gt;中的链接指向哪里,可以查看href属性:它们指向当前页面,ym查询参数设置为$prev$next的值.这就是您在第 6 行的代码检查以确定要显示的月份的内容。
  • so prev(即
  • 查询参数是通过 URL 传递给网页的选项。例如:http://example.com/test.php?something=this&amp;somethingElse=that? 将 URL 与参数分开,&amp; 将一个参数与另一个分开。在 PHP 中,可以使用$_GET 读取这些查询参数。在此示例中,$_GET['something'] 将是“this”,$_GET['somethingElse'] 将是“that”。在您的脚本中,$prev 是上个月(例如“2020-10”),因此 &lt; 链接正在使用 ?ym=2020-10 加载当前 URL,并且您的脚本正在第 6 行检查该链接。
  • 感谢 rickdenhaan,正在处理您的评论

标签: php html web calendar


【解决方案1】:

第一:

if (isset($_GET['ym'])) {
    $ym = $_GET['ym'];
} else {
    // This month
    $ym = date('Y-m');
}    
$prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp)));
    $next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));

您正在创建 Y-m 日期,但需要 Ym 格式。

第二: 您正在为您的月份计算创建一种非常复杂的计算方式。 你应该看看datetime classdateinterval classdateperiod class

您可以创建一个简单的概览:

    <?php
if(isset($_GET['ym'])){
    $start  = DateTime::createFromFormat('Y-m-d',$date.'-1');
    }else{
    $start  = new DateTime();
     $start->setDate($start->format('Y'),$start->format('m'),1);
    }
    $start->setTime(00, 00);
    $end = clone $start;
    $end->setDate($end->format('Y'),$end->format('m'),$end->format('t'))->setTime(23, 59);
    $prev = clone $start;
$prev->modify("-1 month");
$next = clone $end;
$next->modify("+1 day");
    $interval = DateInterval::createFromDateString('1 day');
    
    $period     = new DatePeriod($start,$interval,$end);
?>

那么你可以使用:

<a href="?ym=<?php echo $prev->format('Y-m');?>">&lt;</a>
//AND
 <a href="?ym=<?php echo $next->format('Y-m');?>">&gt;</a>

对于循环:

    <?php
foreach($period as $day){
    echo $day->format('Y-m-d');
    }
?>

Code example

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多