2011-07-18 70 views

回答

0

我会做这种方式...

<?php 
// current day to start with 
$start = mktime(0,0,0,date('m'), date('d'), date('Y'));; 

// loop through the current and last four month 
for ($i = 0; $i <=4; $i++) { 
    // calculate the first day of the month 
    $first = mktime(0,0,0,date('m',$start) - $i,1,date('Y',$start)); 

    // calculate the last day of the month 
    $last = mktime(0, 0, 0, date('m') -$i + 1, 0, date('Y',$start)); 

    // now some output... 
    echo date('Y-m-d',$first) . " - ".date('Y-m-d',$last). "<br>"; 
} 

?> 
1
<?php 
// start day of current month 
echo date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00')); 

// end day of current month 
echo date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))); 
?> 

您可以添加或删除个月,而且你会得到准确的结果

2

使用PHP 5.2 +,你可以使用DateTimeDateInterval来解决这个问题:

实施例:

<?php 

$date = new DateTime('first day of this month 3 months ago'); 

// Loop 4 times. 
for ($i = 0; $i < 4; $i++) 
{ 
    echo 'Start: ' . $date->format('Y-m-d') . PHP_EOL; 
    echo 'End: ' . $date->format('Y-m-t') . PHP_EOL; 

    // Add 1 month. 
    $date->add(new DateInterval('P1M')); 
} 
+0

$ currYear =日期( “Y”); \t \t $ currMonth = date(“m”); \t \t \t \t $ monArr = array(); \t \t \t \t \t \t $ \t第一=日期( 'Y-M-d',mktime(0,0,0,$ CURRMONTH,1,$ currYear))。 '00:00:00'; \t \t $ last = date('Y-m-t',mktime(0,0,0,$ currMonth,1,$ currYear))。 '00:00:00'; \t \t $ monArr ['0'] = $ first。 '## ** ##'。 $最后; \t \t $ prevMoFDate = date('Y-m-d',mktime(0,0,0,$ currMonth - 1,1,$ currYear))。 '00:00:00'; \t \t $ prevMoLDate = date('Y-m-t',mktime(0,0,0,$ currMonth - 1,1,$ currYear))。 '00:00:00'; –

相关问题