2012-12-28 15 views
19

Ho cercato tra gli esempi online e li trovo un po 'criptici o eccessivi.

Che cosa devo fare è qualcosa di simile:

$timestamp = time(); 

e poi scoprire se il giorno è un Lunedi o un pugno del mese?

Sono sicuro che è possibile, non sono sicuro di come farlo.

+0

http://php.net/manual/en/class.datetime.php –

+0

si Nedd per interrogare i dati in base a Lunedi o 1 ° o semplicemente per scoprire è che prima o Lunedi? ;) – Xfile

+0

@Xfile Ho solo bisogno di sapere se oggi è lunedì? E in una linea separata ho bisogno di sapere se oggi è il primo del mese. Essenzialmente ci sono dei promemoria che devono essere inviati settimanalmente e mensilmente, ecco perché ne ho bisogno. – Genadinik

risposta

42

In realtà, non hanno bisogno variabile timestamp perché:

Exerpt da date funzione php.net:

Restituisce una stringa formattata in base alla stringa di formato specificata utilizzando il timestamp dell'intero dato o l'ora corrente se nessun timestamp è dato . In altre parole, timestamp è facoltativo e il valore predefinito è di time().

if(date('j', $timestamp) === '1') 
    echo "It is the first day of the month today\n"; 

if(date('D', $timestamp) === 'Mon') 
    echo "It is Monday today\n"; 
+4

'if (date ('w', $ timestamp) === '1')' è migliore. –

+1

perché è meglio? spiegare. Il carattere in formato "D" è più leggibile. –

+2

Penso che in alcune impostazioni locali il risultato di 'date ('D')' può essere lunedì ma non essere letto come ''Mon''. inoltre, il tuo codice non interessa se è leggibile dall'uomo, vuole reagire nel modo corretto se è lunedì o meno. –

1

È possibile utilizzare: strtotime

$firstdaymonth = strtotime('first day this month'); 
+1

scusa, un po 'confuso, come posso inserire "primo giorno di questo mese" in quella funzione? – Genadinik

+3

scrivendo letteralmente "il primo giorno di questo mese". lo strtotime accetta quelli. google it :) –

2

Questo catturerà .. Lunedi dalle mysql

$monday = 1; //tuesday= 2.. sunday = 7 

    AND $monday = (date_format(from_unixtime(your_date_column),'%w')) 

O giorni ..

$day = 1; ///1st in month 

    AND $day = (date_format(from_unixtime(your_date_column),'%d')) 

solo sapere

$date = date("d"); //1st? 
$dayinweek = date("w"); //monday? //as a number in a week what you need more then just "Monday" I guess.. 
6

Questo dovrebbe risolverlo:

$day = date('D'); 
$date = date('d') 
if($day == Mon){ 
    //Code for monday 
} 
if($date == 01){ 
    //code for 1st fo the month 
} 
else{ 
    //not the first, no money for you =/ 
} 
0

Perché $ data può lunedì o domenica. Dovrebbe essere controllare

public function getWeek($date){ 
    $date_stamp = strtotime(date('Y-m-d', strtotime($date))); 

    //check date is sunday or monday 
    $stamp = date('l', $date_stamp);  

    if($stamp == 'Mon'){ 
     $week_start = $date; 
    }else{ 
     $week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp)); 
    } 


    if($stamp == 'Sunday'){ 
     $week_end = $date; 
    }else{ 
     $week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp)); 
    }   
    return array($week_start, $week_end); 
} 
Problemi correlati