2011-11-17 46 views
2
的最后几天0​​

结果:获取前几个月

Array 
(
    [0] => 2011-10-31 
    [1] => 2011-10-01 //Here should be 2011-09-30 
    [2] => 2011-08-31 
    [3] => 2011-07-31 
) 

我想类似下面,由于输出!

Array 
(
    [0] => 2011-10-31 
    [1] => 2011-09-30 
    [2] => 2011-08-31 
    [3] => 2011-07-31 
) 

回答

1
$days_past = 0;  
for($i=0; $i<4; $i++) { 
    $monthArr[] = date("Y-m-d H:i:s", strtotime('2011-10-31 '.$days_past.' days')); 
    $days_past -= cal_days_in_month(CAL_GREGORIAN, 10 - $i, 2011); 
} 
  • 0天
  • 31天
  • 61天 等
+0

这是正确的做法。即使他们已经工作,其他人也只是在努力工作。 – aartist

+0

@aartist:请告诉我为什么我的解决方案不是“正确的做法”。 – Bluewind

+0

@蓝风让2011-10-24。您的解决方案错了。 – Peter

6
strtotime('2011-10-31 -1 month'); 

这等同于2011年9月31日,其不存在,并且因此将其更改为2011年9月30日之后的2011-10-01。

而不是通过月(其是可变的),试图通过该月的第一天的循环,然后计算该月的最后一天的最后一天循环,即

date('Y-m-t', strtotime('2011-10-01 '. -1*$i .' month')); 

编辑: date()函数的“t”标志返回月份的最后一天。

+0

这是一个很好的解决方法! – Shattuck

+1

是的,但有两件事情:1)'$ 1'不,2)它应该提到的是't'在'日期()有效的变量名(在这种情况下,它应该是'$ i',我相信) '参数是通过返回给定月份的天数来解决问题的线索。 – Tadeck

+0

@Tadeck很好的地方。根据您的建议更新了帖子,并修复了错字;-) – liquorvicar

0

我想你想的mktime()函数:

http://us2.php.net/mktime

// I'm assuming today's date here, but adjust accordingly 
$start_day = date('j'); 
$start_month = date('n'); 
$start_year = date('Y'); 
for($i=0; $i<4; $i++) 
{ 
    $monthArr[] = date("Y-m-d H:i:s", mktime(1, 1, 1, $start_day, $start_month - $i, $start_year)); 
} 
1

PHP有DateTime类可用它应使用的datestrtotime功能,因为你可能意味着使用什么时区因此,当涉及到DST和其他许多事情时,您可以是“安全的”。

如何使用它来解决你的问题:

$date = '2011-11-30'; // our date that we deal with 

$tz = new DateTimeZone('Europe/London'); 

// create new DateTime object specifying the format and the date we create from as well as the time zone we're in 
$dt = DateTime::createFromFormat('Y-m-d', $date, $tz); 

// let's substract 1 month from current DateTime object  
$sub = $dt->sub(new DateInterval('P1M')); // P stands for PERIOD, 1 stands for ONE and M stands for MONTH - so it means we're substracting period of 1 month 

// output the new date 

echo $sub->format('Y-m-d); 
+0

如果运行PHP> 5.3,请使用+1的方式 –

+1

这无法找到真正的月份最后一天。你只需减去一个月,但如果你从30岁开始,你将永远不会打到31(31天的月份)。 – Bluewind

2
for($i=0; $i<4; $i++) { 
    $monthArr[] = date("Y-m-d H:i:s", strtotime('last day of 2011-10-31 '. -1*$i.' month')); 
} 

,而不是“2011-10-31”,你也可以写“今天”当月。

输出示例:

array(10) { 
    [0]=> 
    string(19) "2011-10-31 00:00:00" 
    [1]=> 
    string(19) "2011-09-30 00:00:00" 
    [2]=> 
    string(19) "2011-08-31 00:00:00" 
    [3]=> 
    string(19) "2011-07-31 00:00:00" 
    [4]=> 
    string(19) "2011-06-30 00:00:00" 
    [5]=> 
    string(19) "2011-05-31 00:00:00" 
    [6]=> 
    string(19) "2011-04-30 00:00:00" 
    [7]=> 
    string(19) "2011-03-31 00:00:00" 
    [8]=> 
    string(19) "2011-02-28 00:00:00" 
    [9]=> 
    string(19) "2011-01-31 00:00:00" 
} 
0

这是否帮助:

 
for($i = 0, $j=11; $i < 4; $i++) { 

    echo date("Y-m-d", strtotime("-1 day", strtotime(date("Y-$j-01")))); 
    echo "<br />"; 
    $j--; 
} 
0

我喜欢PHP DateTime类了。当使用它时,每件事都变得简单和简单。

// Get last day of previous month 
$date = new DateTime(); 
$dayOfMonth = $date->format('j'); 
$date->sub(new DateInterval('P' . $dayOfMonth . 'D')); 
echo $date->format('Y-m-d'); 

,如果你只是想拿到最后一个月的,为什么不只是单纯的使用数组,因为只有二月需要粘性护理,其他月份都有一个固定的最后一天。

当您想要获取一周的开始日期和结束日期时,您可能需要DateTime。