$year = 2010;
$month = 10;
Làm cách nào để có được tháng trước 2010-09
và tháng tiếp theo 2010-11
?Cách nhận tháng trước và tháng tiếp theo?
$year = 2010;
$month = 10;
Làm cách nào để có được tháng trước 2010-09
và tháng tiếp theo 2010-11
?Cách nhận tháng trước và tháng tiếp theo?
$date = mktime(0, 0, 0, $month, 1, $year);
echo strftime('%B %Y', strtotime('+1 month', $date));
echo strftime('%B %Y', strtotime('-1 month', $date));
$prevMonth = $month - 1;
$nextMonth = $month + 1;
$prevYear = $year;
$nextYear = $year;
if ($prevMonth < 1) {
$prevMonth = 1;
$prevYear -= 1;
}
if ($nextMonth > 12) {
$nextMonth = 1;
$nextYear += 1
}
hoặc
// PHP > 5.2.0
$date = new DateTime();
$date->setDate($year, $month, 1);
$prevDate = $date->modify('-1 month');
$nextDate = $date->modify('+1 month');
// some $prevDate->format() and $nextDate->format()
Bạn chỉ có thể thêm 1
đến tháng hiện tại và sau đó xem nếu bạn vượt qua năm:
$next_year = $year;
$next_month = ++$month;
if($next_month == 13) {
$next_month = 1;
$next_year++;
}
Tương tự cho tháng trước bạn có thể làm :
$prev_year = $year;
$prev_month = --$month;
if($prev_month == 0) {
$prev_month = 12;
$prev_year--;
}
PHP là tuyệt vời trong lĩnh vực này, nó sẽ xử lý tràn cập nhật bằng cách sửa ngày cho bạn ...
$PreviousMonth = mktime(0, 0, 0, $month - 1, 1, $year);
$CurrentMonth = mktime(0, 0, 0, $month, 1, $year);
$NextMonth = mktime(0, 0, 0, $month + 1, 1, $year);
echo '<p>Next month is ' . date('Ym', $NextMonth) .
' and previous month is ' . date('Ym', $PreviousMonth . '</p>';
strftime * : - * Định dạng thời gian và/hoặc ngày theo cài đặt ngôn ngữ. Các tên tháng và các ngày trong tuần và các chuỗi phụ thuộc ngôn ngữ khác tôn trọng tập hợp miền địa phương hiện tại với setlocale().
strftime('%B %Y', strtotime('+1 month', $date));
strftime('%B %Y', strtotime('-1 month', $date));
thử nó như thế này:
$date = mktime(0, 0, 0, $month, 1, $year);
echo date("Y-m", strtotime('-1 month', $date));
echo date("Y-m", strtotime('+1 month', $date));
hay, ngắn hơn, như thế này:
echo date("Y-m", mktime(0, 0, 0, $month-1, 1, $year));
echo date("Y-m", mktime(0, 0, 0, $month+1, 1, $year));
echo date('Y-m-d', strtotime('next month'));
setlocale(LC_TIME,"turkish");
$Currentmonth=iconv("ISO-8859-9","UTF-8",strftime('%B'));
$Previousmonth=iconv("ISO-8859-9","UTF-8",strftime('%B',strtotime('-1 MONTH')));
$Nextmonth=iconv("ISO-8859-9","UTF-8",strftime('%B',strtotime('+1 MONTH')));
echo $Previousmonth; // Şubat /* 2017-02 */
echo $Currentmonth; // Mart /* 2017-03 */
echo $Nextmonth; // Nisan /* 2017-04 */
Ông có thể giải thích rõ hơn! – Trufa