2009-10-20 10 views

Respuesta

22

para la representación textual completa del mes en el que tiene que pasar "F":

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

para el mes anterior se puede utilizar

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

+0

Tyler gracias .. i necesidad éste ... – Fero

+4

Este fallará cuando se realiza el día 31. Ver mi respuesta – Yarin

0

Tendrá que usar la fecha ("F"); para obtener la representación de texto completo de la fecha.

6

este mes

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

meses anteriores

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

el suyo esta respuesta produce ERROR DE PARÁLISIS Parse error: error de sintaxis, inesperado ';' en E: \ Archivos de programa \ xampp \ htdocs \ test_Fero \ test.php en la línea 3 – Fero

+0

Si cuenta los corchetes de apertura '(' encontrará que hay cinco en cada línea, pero solo cuatro corchetes de cierre. equilibre los dos, cinco de cada uno, el código debería funcionar. –

+0

Parece que necesita otro cierre ")" antes del punto y coma. –

2

intente utilizar el construido en strtotime función en PHP y el uso de 'F' para la plena salida de texto:

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

Recordando que" último "es simplemente un sinónimo de" -1 ", no debe doblar en strtotime() cuando no es necesario; esto es solo perder ciclos. –

+0

¿Qué tiene de malo? Está funcionando como se requiere :) – vava

+0

@Nathan Kleyn, no tuve la sintaxis de "-1 mes", "el mes pasado" fue solo una suposición que funcionó. – vava

2

Si quieres ser OO P sobre ella, intente esto:

$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" 

salidas:

  • 09: Oct: 20
  • 09: Sep: 20
  • 09: Aug: 20
12

reloj por el FUAH! Las otras respuestas fallarán cuando se realicen el 31 del mes. Use este lugar:

/* 
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>"; 

Para más información, véase mi respuesta 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 
Cuestiones relacionadas