【问题标题】:PHP get next month issuePHP得到下个月的问题
【发布时间】:2017-01-15 20:25:54
【问题描述】:

我正在尝试通过 php 获取当前、下个月和下个月的下个月。

这是我当前的 php:

setlocale(LC_TIME, "de_DE.utf8");
$thismonth = strftime("%B");
$nextmonth = strftime( '%B', strtotime( '+1 month', mktime( 0, 0, 0, $month, 1, $year ) ) );
$overnextmonth = strftime( '%B', strtotime( '+2 month', mktime( 0, 0, 0, $month, 1, $year ) ) );

月份名称应该是德语所以我使用setlocale(LC_TIME, "de_DE.utf8");

输出为:Januar(一月)、Januar(一月)、Februar(二月)。我无法解决这个问题。

【问题讨论】:

标签: php time strftime


【解决方案1】:

使用这个setlocale (LC_TIME,"de_DE");它会工作

<?php
setlocale (LC_TIME,"de_DE"); 
echo $thismonth = strftime("%B");
echo $nextmonth = strftime( '%B', strtotime( '+1 month', mktime( 0, 0, 0, 1, 1, 2017 ) ) );
echo $overnextmonth = strftime( '%B', strtotime( '+2 month', mktime( 0, 0, 0, 2, 1, 2017 ) ) );

?>

输出

output picture

【讨论】:

  • 而不是 $month 和 $year 我把正常值 2017 年和 1 月
【解决方案2】:

我建议远离 strtotime 并使用 php dateTime 类。

$date = new DateTime();
$date->add(new DateInterval('P1M')); // <-- adds 1 month to the current
echo $date->format('F') . "\n"; // it's now January so it outputs February
$date->add(new DateInterval('P1M'));
echo $date->format('F') . "\n"; // adds one more (total 2) months from original

当前月份是一月。此输出将是“二月三月”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多