简体   繁体   English

获取当前月份的月份名称-到PHP年底

[英]Get month names from current month - to the end of the year in PHP

I have fetched a current month from my DB which is basically a join date of the user. 我从数据库中获取了当前月份,这基本上是用户的加入日期。 Lets say the use joined this month and it is May. 可以说使用加入了本月,现在是5月。 The code I do to fetch the month name is like this: 我获取月份名称的代码如下所示:

$months = array();
array_push($months,date("F",strtotime($me['joinTime'])));

In this case I add the start month to the array, which in this case is May... Now what I'd like to do is as the months go by, I'd like to add each new month to the array.. So for instance in a few days its June, and when June kicks in, I'll add that Month as well to the array.. So my question here is, how can I get the rest of the month names from the start date (May). 在这种情况下,我将开始月份添加到数组中,在这种情况下是5月...现在,我想做的是随着月份的过去,我想将每个新月添加到数组中。因此,例如,在六月的几天内,以及六月开始的时候,我也会将该月份也添加到数组中。因此,我的问题是,如何从开始日期获得月份的其余名称(可以)。

I need June, July, August, September, October, November, December... 我需要六月,七月,八月,九月,十月,十一月,十二月...

If the start month was April I'd add May into the array as well... 如果开始月份是四月,我也将五月添加到数组中...

Can someone help me out with this ? 有人可以帮我这个忙吗?

First you need to get he month number and than you need to use a loop through to end of the year that is 12. For each month number you also need the month name so use DateTime createFromFormat . 首先,您需要获取他的月份号,然后需要使用循环到年底的12。对于每个月份的数字,您还需要月份名称,因此请使用DateTime createFromFormat

Online Check 在线查询

$months = array();
$num = date("n",strtotime($me['joinTime']));
array_push($months, date("F", strtotime('2016-05-17 16:41:51')));

for($i = ($num + 1); $i <= 12; $i++){
    $dateObj = DateTime::createFromFormat('!m', $i);
    array_push($months, $dateObj->format('F'));
}

print_r($months); // Array ( [0] => May [1] => June [2] => July [3] => August [4] => September [5] => October [6] => November [7] => December )

Yo can also put it like can也可以这样说

$array = array();
array_push($array, date('F')) ;
for ($i=1; $i<=  12 - date('m'); $i++ ){
    array_push($array, date('F', strtotime("+$i months"))) ;
}
print "<pre>";print_r($array);

Here we will be using DatePeriod which allows iteration over a set of dates and times, recurring at regular intervals, over a given period. 在这里,我们将使用DatePeriod,它允许在给定时间段内按固定间隔重复的一组日期和时间进行迭代。

So we got the end date and we have the start date and then calculated the interval. 这样我们得到了结束日期,有了开始日期,然后计算了间隔。 And then looping over the period we got the array of months. 然后循环遍历整个月。

// current date : 20 Feb 2019

$startDate = new \DateTime('first day of next month');
$endDate = new \DateTime('1st january next year');

$interval = new \DateInterval('P1M');

$period = new \DatePeriod($startDate, $interval, $endDate);

// Start array with current date
$dates = [];

// Add all remaining dates to array
foreach ($period as $date) {
     array_push($dates, $date->Format('F'));
}

// output
print_r($dates); die;

Array ( [0] => March [1] => April [2] => May [3] => June [4] => July [5] => August [6] => September [7] => October [8] => November [9] => December )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM