我在我的 MySQL 数据库中创建了一个表,其中包含整整一个月的日期。
然后我尝试将它们显示在表格中(看起来像日历)
我创建了一个脚本,所以该月的第一天显示在正确的日期(例如,星期六表示十月)。
然后我会在它之后显示所有其他日期。
mã số
$Firstdate = date('Y-m-01');
$FirstDay = date("N", strtotime($Firstdate));
if ($FirstDay == 1) {}
if ($FirstDay == 2) { echo" | ";}
if ($FirstDay == 3) { echo" | | ";}
if ($FirstDay == 4) { echo" | | | ";}
if ($FirstDay == 5) { echo" | | | | ";}
if ($FirstDay == 6) { echo" | | | | | ";}
if ($FirstDay == 7) { echo" | | | | | | ";}
$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 " $TheDateF
| ";
if ($i % 7 == 0) {
echo ''; // it's time no move to next row
}
}
这在正确的一天开始,但是请看下面的截图,我需要它在星期日日期之后开始新的一行:
有什么办法可以解决这个问题吗?
试试这段代码。根据您的需要进行修改。我很确定这会对您有所帮助。
/* 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"));
?>
来源 http://code.runnable.com/VKxpI5dzCMkTrRq1/simple-calendar-for-php
Tôi là một lập trình viên xuất sắc, rất giỏi!