如何通过 PHP 显示下个月和上个月
How to display next and previous month via PHP
我只是想为此数组添加功能,以便在点击时显示下个月。它目前显示当月的所有天数。
我的代码:
<?php
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
$next_date = date('n', strtotime($month .' +1 month'));
$prev_date = date('n', strtotime($month .' -1 month'));
//loop through all days
for ($i = 1; $i <= $day_count; $i++)
{
$date = $year.$month.$i; //format date
$get_name = date('l', strtotime($date)); //get week day
$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
//if not a weekend add day to array
if($day_name != 'Sun' && $day_name != 'Sat')
{
$workdays[] = $i;
}
}
foreach ($workdays as $workday => $test) {
echo $test;
}
?>
输出:
3 4 5 6 7 10 11 12 13 14 17 18 19 20 21 24 25 26 27 28
我需要这样的东西:
<a href="?date=<?=$prev_date;?>">Previous month</a>
<a href="?date=<?=$next_date;?>">Next month</a>
根据我的理解,您需要显示一些 link 到当前月份的工作日,以及一个 link 到上个月和下个月。
The below code requires PHP5.4 or higher.
以下函数将 return link 秒到一个月中的工作日:
const DATE_FOMAT = 'Y-m-d';
const LINK_SEPARATOR = " | ";
// get the reference date from the Query string so that we can print the calendar for any month
$refDateStr = $_GET['dt'] ?? date(DATE_FOMAT); // this requires PHP 7.1 or above
// this function will return the links to all working days
// in the given month (based on the $refDateStr)
function getDateLinks($refDateStr) {
try {
// first we check if the given date is valid or not
$refDateDt = date_create_from_format(DATE_FOMAT, $refDateStr);
if (!$refDateDt || $refDateDt->format(DATE_FOMAT) !== $refDateStr) {
// date is not valid. We cannot proceed with this date
throw new Exception('Invalid date supplied. Dates should be in ' . DATE_FOMAT .'.');
}
// now lets find first and last days of the given month
// for convenience, the start date is set to the last date of past month
$startDate = new DateTime($refDateDt->modify('first day of this month')->format(DATE_FOMAT));
$startDate->sub(new DateInterval("P1D"));
$endDate = new DateTime($refDateDt->modify('last day of this month')->format(DATE_FOMAT));
$dateLink = null;
// lets create the date links
while ($startDate < $endDate) {
$startDate->add(new DateInterval("P1D"));
// exclude weekends... :-)
if (in_array($startDate->format('D'), ['Sat', 'Sun']))
continue;
$dateLink .= getDateLinksFormated($startDate);
}
return rtrim($dateLink, LINK_SEPARATOR);
} catch (Exception $exception) {
echo $exception->getMessage();
}
}
让我们决定如何显示日期 links
// This function create the date links
function getDateLinksFormated(DateTime $dt):string
{
return sprintf(
"<a href='?%s'>%s</a>%s",
$dt->format(DATE_FOMAT),
$dt->format('d'),
LINK_SEPARATOR
);
}
现在是展示日历的时候了!!!
// Link to the past month
echo "<a href='?dt=" . date(DATE_FOMAT, strtotime('-1 month', strtotime($refDateStr))) . "'><< PREV MONTH </a>";
// Link to the next month
echo getDateLinks($refDateStr);
// Link to the next month
echo "<a href='?dt=" . date(DATE_FOMAT, strtotime('+1 month', strtotime($refDateStr))) . "'> NEXT MONTH >></a>";
Read more about PHP DateTime Class here
我只是想为此数组添加功能,以便在点击时显示下个月。它目前显示当月的所有天数。
我的代码:
<?php
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
$next_date = date('n', strtotime($month .' +1 month'));
$prev_date = date('n', strtotime($month .' -1 month'));
//loop through all days
for ($i = 1; $i <= $day_count; $i++)
{
$date = $year.$month.$i; //format date
$get_name = date('l', strtotime($date)); //get week day
$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
//if not a weekend add day to array
if($day_name != 'Sun' && $day_name != 'Sat')
{
$workdays[] = $i;
}
}
foreach ($workdays as $workday => $test) {
echo $test;
}
?>
输出:
3 4 5 6 7 10 11 12 13 14 17 18 19 20 21 24 25 26 27 28
我需要这样的东西:
<a href="?date=<?=$prev_date;?>">Previous month</a>
<a href="?date=<?=$next_date;?>">Next month</a>
根据我的理解,您需要显示一些 link 到当前月份的工作日,以及一个 link 到上个月和下个月。
The below code requires PHP5.4 or higher.
以下函数将 return link 秒到一个月中的工作日:
const DATE_FOMAT = 'Y-m-d';
const LINK_SEPARATOR = " | ";
// get the reference date from the Query string so that we can print the calendar for any month
$refDateStr = $_GET['dt'] ?? date(DATE_FOMAT); // this requires PHP 7.1 or above
// this function will return the links to all working days
// in the given month (based on the $refDateStr)
function getDateLinks($refDateStr) {
try {
// first we check if the given date is valid or not
$refDateDt = date_create_from_format(DATE_FOMAT, $refDateStr);
if (!$refDateDt || $refDateDt->format(DATE_FOMAT) !== $refDateStr) {
// date is not valid. We cannot proceed with this date
throw new Exception('Invalid date supplied. Dates should be in ' . DATE_FOMAT .'.');
}
// now lets find first and last days of the given month
// for convenience, the start date is set to the last date of past month
$startDate = new DateTime($refDateDt->modify('first day of this month')->format(DATE_FOMAT));
$startDate->sub(new DateInterval("P1D"));
$endDate = new DateTime($refDateDt->modify('last day of this month')->format(DATE_FOMAT));
$dateLink = null;
// lets create the date links
while ($startDate < $endDate) {
$startDate->add(new DateInterval("P1D"));
// exclude weekends... :-)
if (in_array($startDate->format('D'), ['Sat', 'Sun']))
continue;
$dateLink .= getDateLinksFormated($startDate);
}
return rtrim($dateLink, LINK_SEPARATOR);
} catch (Exception $exception) {
echo $exception->getMessage();
}
}
让我们决定如何显示日期 links
// This function create the date links
function getDateLinksFormated(DateTime $dt):string
{
return sprintf(
"<a href='?%s'>%s</a>%s",
$dt->format(DATE_FOMAT),
$dt->format('d'),
LINK_SEPARATOR
);
}
现在是展示日历的时候了!!!
// Link to the past month
echo "<a href='?dt=" . date(DATE_FOMAT, strtotime('-1 month', strtotime($refDateStr))) . "'><< PREV MONTH </a>";
// Link to the next month
echo getDateLinks($refDateStr);
// Link to the next month
echo "<a href='?dt=" . date(DATE_FOMAT, strtotime('+1 month', strtotime($refDateStr))) . "'> NEXT MONTH >></a>";
Read more about PHP DateTime Class here