2011-07-18 2 views

Répondre

0

que je le ferais de cette façon ...

<?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')))); 
?> 

vous pouvez ajouter ou supprimer des mois ... et vous obtiendrez des résultats précis

2

Avec PHP 5.2+, vous pouvez utiliser DateTime et DateInterval pour résoudre ce problème:

Exemple:

<?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 = Date ("Y"); \t \t $ currMonth = date ("m"); \t \t \t \t $ monArr = array(); \t \t \t \t \t \t \t $ first = Date ('Y-m-d', mktime (0, 0, 0, currMonth $, 1, currYear $)). '00:00:00'; $ Last = date ('Y-m-t', mktime (0, 0, 0, $ currMonth, 1, $ currYear)) \t \t '00:00:00'; \t \t $ monArr ['0'] = $ premier. '## ** ##'. $ last; \t \t $ prevMoFDate = Date ('Y-m-d', mktime (0, 0, 0, currMonth $ - 1, 1, currYear $)). '00:00:00'; $ PrevMoLDate = date ('Y-m-t', mktime (0, 0, 0, $ currMonth-1, 1, $ currYear)) \t '00:00:00'; –

Questions connexes