2009-10-20 14 views

risposta

22

per rappresentazione testuale completa di mese è necessario passare "F":

echo date("y:F:d"); 

per mese precedente è possibile utilizzare

echo date("y:F:d",strtotime("-1 Months"));

+0

grazie Tyler .. ho bisogno di questo ... – Fero

+4

Questo fallirà se effettuata il 31. Vedi la mia risposta. – Yarin

0

Avrete bisogno di usare la data ("F"); per ottenere la rappresentazione completa del testo della data.

6

questo mese

date("y:M:d", mktime(0, 0, 0, date('m'), date('d'), date('Y'))); 

mesi precedenti

date("y:M:d", mktime(0, 0, 0, date('m') - 1, date('d'), date('Y'))); 
date("y:M:d", mktime(0, 0, 0, date('m') - 2, date('d'), date('Y'))); 
+0

questa risposta produce PARSE ERRORE Errore di analisi: errore di sintassi, imprevisto ';' in E: \ Programmi \ xampp \ htdocs \ test_Fero \ test.php sulla riga 3 – Fero

+0

Se contate le parentesi di apertura '(' troverete che ci sono cinque in ogni riga, ma solo quattro parentesi di chiusura. bilancia i due, cinque di ciascuno, il codice dovrebbe funzionare. –

+0

Sembra che tu abbia bisogno di un'altra chiusura ")" prima del punto e virgola. –

2

provare a utilizzare il costruito in strtotime funzione in PHP e usando 'F' per output testuale completa:

echo date('y:F:d'); // first month 
echo date('y:F:d', strtotime('-1 month')); // previous month 
echo date('y:F:d', strtotime('-2 month')); // second previous month 
echo date('y:F:d', strtotime('-3 month')); // third previous month 
-1
echo date('F', strtotime('-2 month')), '<br>', 
    date('F', strtotime('last month')), '<br>', 
    date('F'); 
+0

Ricordando che" last "è semplicemente un sinonimo di" -1 ", non si dovrebbe raddoppiare su strtotime() quando non è necessario; questo è solo sprecare cicli. –

+0

Cosa c'è di sbagliato in questo? Funziona come richiesto :) – vava

+0

@Nathan Kleyn, non ho parlato della sintassi "-1 mese", "il mese scorso" era solo un'ipotesi che funzionava. – vava

2

Se vuoi essere OO P a questo proposito, provate questo:

$dp=new DatePeriod(date_create(),DateInterval::createFromDateString('last month'),2); 
foreach($dp as $dt) echo $dt->format("y:M:d"),"\n"; //or "y F d" 

uscite:

  • 09: Ott: 20
  • 09: Set: 20
  • 09: Ago: 20
12

Guarda fuori per il FUAH! Le altre risposte falliranno se eseguite il 31 del mese. Usare questo invece:

/* 
Handles month/year increment calculations in a safe way, 
avoiding the pitfall of 'fuzzy' month units. 

Returns a DateTime object with incremented month values, and a date value == 1. 
*/ 
function incrementDate($startDate, $monthIncrement = 0) { 

    $startingTimeStamp = $startDate->getTimestamp(); 
    // Get the month value of the given date: 
    $monthString = date('Y-m', $startingTimeStamp); 
    // Create a date string corresponding to the 1st of the give month, 
    // making it safe for monthly calculations: 
    $safeDateString = "first day of $monthString"; 
    // Increment date by given month increments: 
    $incrementedDateString = "$safeDateString $monthIncrement month"; 
    $newTimeStamp = strtotime($incrementedDateString); 
    $newDate = DateTime::createFromFormat('U', $newTimeStamp); 
    return $newDate; 
} 

$currentDate = new DateTime(); 
$oneMonthAgo = incrementDate($currentDate, -1); 
$twoMonthsAgo = incrementDate($currentDate, -2); 
$threeMonthsAgo = incrementDate($currentDate, -3); 

echo "THIS: ".$currentDate->format('F Y') . "<br>"; 
echo "1 AGO: ".$oneMonthAgo->format('F Y') . "<br>"; 
echo "2 AGO: ".$twoMonthsAgo->format('F Y') . "<br>"; 
echo "3 AGO: ".$threeMonthsAgo->format('F Y') . "<br>"; 

Per di più, vedi la mia risposta here

-4
DECLARE @STARTDATE VARCHAR(MAX) 
DECLARE @ENDDATE VARCHAR(MAX) 
DECLARE @A INT 
SET @A=-12 
SET @STARTDATE= DATENAME(MONTH,GETDATE())+ DATENAME(YEAR, DATEADD(MONTH,0,GETDATE())) 
WHILE @A < 0 
BEGIN 
SET @STARTDATE = DATENAME(MONTH,DATEADD(MONTH,@A,GETDATE()))+' '+ DATENAME(YEAR, DATEADD(MONTH,@A,GETDATE())) 
SET @[email protected]+1 
PRINT @STARTDATE 
END 
0
DECLARE @STARTDATE INT 

SET @STARTDATE = -12 

WHILE @STARTDATE < 0 

BEGIN 

PRINT DATENAME(MONTH,DATEADD(MM,@STARTDATE,GETDATE()))+' '+ DATENAME(YEAR, DATEADD(MM,@STARTDATE ,GETDATE())) 

SET @STARTDATE [email protected]+1 

END 
Problemi correlati