php - 如何在两个日期之间找到月份名称和数字?

时间:2016-11-10 11:42:24

标签: php

我需要找到两个日期之间的月份名称和数字      对于上述两个变量,如何找出月份数,即输出将如下所示

  

aug2016,sep2016,oct2016,nov2016,dec2016,jan2017

3 个答案:

答案 0 :(得分:1)

<?php

$date1  = '2013-11-15';
$date2  = '2014-02-15';
$output = [];
$time   = strtotime($date1);
$last   = date('M-Y', strtotime($date2));

do {
$month = date('M-Y', $time);
$total = date('t', $time);

$output[] = $month;

$time = strtotime('+1 month', $time);
} while ($month != $last);

echo implode(",", $output);
?>

输出

Nov-2013,Dec-2013,Jan-2014,Feb-2014 

答案 1 :(得分:1)

试试这个

<?php 
 $var1 = "2016-08-15";
 $var2 = "2017-01-31";
 $result = '';
 while (date('m', strtotime($var1)) != date('m', strtotime($var2))) {
    $result .= date('MY',(strtotime('next month',strtotime($var1)))).",";   
    $var1 = date('Y-m-d',(strtotime('next month',strtotime($var1))));
 }
 echo trim($result, ",");
?>

产出:2016年9月,2016年10月,2016年11月,2016年12月,2016年1月

答案 2 :(得分:0)

如果我错了,请纠正我,但根据您提供的输出,您真正想要做的就是从月份编号中提取月份名称。

请参阅:http://www.php.net/manual/en/function.jdmonthname.php

$monthName = jdmonthname($month, 0);

我认为你应该可以从那里拿走它。