2010-12-14 15 views
13

Eventuali duplicati:
Get first day of week in PHP?Trovare Primo giorno della settimana tramite PHP

Ciao,

voglio trovare prima e l'ultima data di settimana in corso e la scorsa settimana. Analogamente, desidero trovare la prima e l'ultima data del mese corrente e del mese scorso.

Questo deve essere fatto in PHP. Per favore aiuto.

+0

Vedi anche: http://stackoverflow.com/questions/1897727/get-first -day-of-week-in-php –

+3

* (riferimento) * [Formati data relativi in ​​PHP] (http://de2.php.net/manual/en/datetime.formats.relative.php) – Gordon

risposta

42

strtotime è abbastanza potente, con relative time formats:

strtotime('monday this week'); 
strtotime('sunday this week'); 
strtotime('monday last week'); 
strtotime('sunday last week'); 

(questo funziona solo con PHP 5.3+)

strtotime('first day of this month'); 
strtotime('last day of this month'); 
strtotime('first day of last month'); 
strtotime('last day of last month'); 

Al fine di ottenere la prima e l'ultima data di un mese in PHP < 5.3, è possibile utilizzare una combinazione di mktime e date (date('t') indica il numero di giorni del mese):

mktime(0,0,0,null, 1); // gives first day of current month 
mktime(0,0,0,null, date('t')); // gives last day of current month 

$lastMonth = strtotime('last month'); 
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month 
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month 

Se si desidera solo per ottenere una stringa per la presentazione, quindi non è necessario mktime:

date('Y-m-1'); // first day current month 
date('Y-m-t'); // last day current month 
date('Y-m-1', strtotime('last month')); // first day last month 
date('Y-m-t', strtotime('last month')); // last day last month 
+0

Hai fornito un'ottima soluzione, ma sfortunatamente funziona solo su PHP5.3 non in 5.2. Ho PHP 5.2.14. E questo non ha funzionato! –

+1

@ user365870: Vero, non è documentato da nessuna parte, l'ho appena letto nel commento della pagina .... comunque, guarda la mia risposta aggiornata. –

+0

Grazie! Questo ha funzionato perfettamente. Molte grazie. –

3

Ecco una funzione per il primo e l'ultimo giorno della settimana:

function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y') 
{ 
    $wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101')); 
    $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts); 
    return date($format, $mon_ts); 
} 

$sStartDate = week_start_date($week_number, $year); 
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate))); 

Probabilmente può essere adattato a fare il mese pure, ma ho voluto ottenere la mia risposta in! :)

+0

Ho effettivamente trovato il post del forum fa riferimento a Googling da solo, grazie :) ... non ha nemmeno visto la sua risposta da quell'altro thread di stackoverflow ... – prilldev

+0

Ok, quindi ritiro il mio commento :) –

+0

Questo ha funzionato anche per me. Grazie –

Problemi correlati