2014-09-18 11 views
18

Ho bisogno di utilizzare PHP DateTime per ottenere il primo giorno dell'anno corrente. Ho provato:Come ottenere il primo giorno dell'anno in corso?

$year = new DateTime('first day of this year'); 
var_dump($year); 

ma questo sembra essere di ritorno il primo giorno del mese corrente: 2014-09-01 09:28:56

Perché? Come ottengo correttamente il primo giorno dell'anno in corso?

+9

Soluzione '$ anno = nuovo DateTime ('primo giorno di gennaio');' non ok? –

+3

Inoltre, questo dovrebbe funzionare anche 'new \ DateTime ('primo giorno di gennaio di quest'anno')' o 'new \ DateTime ('ultimo giorno di dicembre del prossimo anno')' ad esempio ... – Tecnocat

risposta

20
echo date('l',strtotime(date('Y-01-01'))); 
8

È possibile ottenere la data corrente e quindi impostare il giorno e mese a 1:

$year = new DateTime(); 
$year->setDate($year->format('Y'), 1, 1); 

Opzionalmente, è possibile impostare il tempo di mezzanotte:

$year->setTime(0, 0, 0); 
1

Date un'occhiata a questo collegamento - http://davidhancock.co/2013/11/get-the-firstlast-day-of-a-week-month-quarter-or-year-in-php/

function firstDayOf($period, DateTime $date = null) 
{ 
    $period = strtolower($period); 
    $validPeriods = array('year', 'quarter', 'month', 'week'); 

    if (! in_array($period, $validPeriods)) 
     throw new InvalidArgumentException('Period must be one of: ' . implode(', ', $validPeriods)); 

    $newDate = ($date === null) ? new DateTime() : clone $date; 

    switch ($period) { 
     case 'year': 
      $newDate->modify('first day of january ' . $newDate->format('Y')); 
      break; 
     case 'quarter': 
      $month = $newDate->format('n') ; 

      if ($month < 4) { 
       $newDate->modify('first day of january ' . $newDate->format('Y')); 
      } elseif ($month > 3 && $month < 7) { 
       $newDate->modify('first day of april ' . $newDate->format('Y')); 
      } elseif ($month > 6 && $month < 10) { 
       $newDate->modify('first day of july ' . $newDate->format('Y')); 
      } elseif ($month > 9) { 
       $newDate->modify('first day of october ' . $newDate->format('Y')); 
      } 
      break; 
     case 'month': 
      $newDate->modify('first day of this month'); 
      break; 
     case 'week': 
      $newDate->modify(($newDate->format('w') === '0') ? 'monday last week' : 'monday this week'); 
      break; 
    } 

    return $newDate; 
} 
8

Se si desidera ottenere primo giorno dell'anno in corso basta usare questo codice

echo date("l", strtotime('first day of January '.date('Y'))); 
21

tuo parente formato della data 'first day of this year' è corretto restituendo il primo giorno del mese a causa della definizione di first day of:

Sets il giorno del primo del mese corrente. Questa frase è la migliore utilizzata insieme al nome del mese successivo. (See PHP-doc)

per ottenere il primo giorno dell'anno corrente con il formato relativo si può usare qualcosa di simile:

'first day of January ' . date('Y') 
+7

o meglio: strtotime (' primo giorno di gennaio di quest'anno ') –

1

in PHP 5.3.10 questo funziona

$myDate = new \DateTime(date("Y")."-01-01");                                           
echo $myDate->format("Y-m-d"); 

In PHP 5.4 e in alto puoi mettere tutti insieme

echo (new \DateTime(date("Y")."-01-01"))->format("Y-m-d") 
2

Prova questo:

date('Y-m-d', strtotime('first day of january this year')); 
1

Come un commentatore @ Glavić detto già

$year = new DateTime('first day of January');

è la soluzione.

Per me aveva senso semanticamente che "quest'anno" dovrebbe tornare a mezzanotte del primo giorno dell'anno, ma in effetti non è così!

Problemi correlati