[英]PHP Get Current year after current month
我正在使用此代码获取当前年份
echo date("Y");
我也有一个变量来表示月份
$month= "November"
现在是11月,我要输入的代码正确的是2016年。
但是,我想在12月获得2017年11月,因为2016年11月已经到期。
我该怎么办?
我的代码:
<?php the_field('fl_month');echo date("Y"); ?>
// 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');
}
因此,我将输入月份和当前月份转换为数字格式,检查当前月份是否大于或等于输入月份,然后根据该值确定是否应该在明年。
循环浏览每个月,直到找到下一个十一月:
$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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.