Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP获取当前月份之后的当前年份_Php_Date - Fatal编程技术网

PHP获取当前月份之后的当前年份

PHP获取当前月份之后的当前年份,php,date,Php,Date,我正在使用此代码获取当前年份 echo date("Y"); 我还有一个变量作为月份的名称 $month= "November" 现在是11月,我得到了2016年,这与我的代码是正确的 但是在12月,我想得到2017年11月,因为2016年11月已经到期 我该怎么做呢 我的代码: <?php the_field('fl_month');echo date("Y"); ?> 每月循环一次,直到找到下一个11月: $month = 'August'; echo myyear($mo

我正在使用此代码获取当前年份

echo date("Y");
我还有一个变量作为月份的名称

$month= "November"
现在是11月,我得到了2016年,这与我的代码是正确的

但是在12月,我想得到2017年11月,因为2016年11月已经到期

我该怎么做呢

我的代码:

<?php the_field('fl_month');echo date("Y"); ?>

每月循环一次,直到找到下一个11月:

$month = 'August';
echo myyear($month); // 2017
$month = 'November';
echo myyear($month); // 2016

function myyear($month) {
  $dt = strtotime($month);
  $target = date('m', $dt);
  if (date('m', time()) == date('m', $dt)) return date('Y');
  do {
    $dt = strtotime('next month', $dt);
  } while ($target != date('m', $dt));
  return date('Y', $dt);
}

因此,我将输入月份和当前月份转换为数字格式,检查当前月份是否大于或等于输入月份,然后根据这一点,我决定是否应该是明年。

您的代码在哪里?刚刚发布,不知道在此还有什么要做的。\u字段表明这是Wordpress应用程序中的代码?正确吗?这是WordPress,因为PHP应该是:echo strftime(“%B%Y”);但不知何故,我不认为这会被翻译成Wordpress,而只是在页面上的某个地方结束。
// incoming month
$month = 'November';
$monthNumber = date("n", strtotime($month));

// current month
$currentMonth = date("n");

if ($currentMonth >= $monthNumber) {
    echo $month . ' ' . date('Y', strtotime('+1 year'));
} else {
    echo $month . ' ' . date('Y');
}