2010-03-25 8 views
8

ho la linea di sotto di codiciZend Data - giorno differenza

$day1 = new Zend_Date('2010-03-01', 'YYYY-mm-dd'); 
$day2 = new Zend_Date('2010-03-05', 'YYYY-mm-dd'); 
$dateDiff = $day2->getDate()->get(Zend_Date::TIMESTAMP) - $day1->getDate()->get(Zend_Date::TIMESTAMP); 
$days = floor((($dateDiff/60)/60)/24); 
return $days; 

questo tornerà 4

Ma se ha dato

$day1 = new Zend_Date('2010-02-28', 'YYYY-mm-dd'); 
$day2 = new Zend_Date('2010-03-01', 'YYYY-mm-dd'); 
$dateDiff = $day2->getDate()->get(Zend_Date::TIMESTAMP) - $day1->getDate()->get(Zend_Date::TIMESTAMP); 
$days = floor((($dateDiff/60)/60)/24); 
return $days; 

tornerà -27 .. come sarà ottengo la risposta giusta

risposta

14
$firstDay = new Zend_Date('2010-02-28', 'YYYY-MM-dd'); 
$lastDay = new Zend_Date('2010-03-01', 'YYYY-MM-dd'); 
$diff = $lastDay->sub($firstDay)->toValue(); 
$days = ceil($diff/60/60/24) +1; 

ritorno $ giorni;

questo dà la risposta giusta

7

Credo che il problema sia nella vostra parte stringa. Prova invece a YYYY-MM-dd.

$day1 = new Zend_Date('2010-02-28', 'YYYY-MM-dd'); 
$day2 = new Zend_Date('2010-03-01', 'YYYY-MM-dd'); 
echo $day2->sub($day1)->toString(Zend_Date::DAY); 
+0

Grazie Mike .. sta funzionando ... ma è necessario sottrarre 1;) –

+5

Attenzione 'YYYY' è l'anno ISO. Usa 'aaaa' per l'anno. – smack0007

+0

Zend Framework Date :: sub() restituisce una differenza in secondi e non un oggetto toString non funzionerà. – kevin

2

ho esteso Zend_Date per le mie funzioni comfort. La mia soluzione è simile a Nisanth di, con alcune differenze fondamentali:

  1. calcolare l'inizio della giornata per entrambi i giorni prima di confrontare
  2. uso round() invece di ceil()
  3. non aggiungere 1 al risultato
codice

Esempio:

class My_Date extends Zend_Date 
{ 
    public static function now($locale = null) 
    { 
     return new My_Date(time(), self::TIMESTAMP, $locale); 
    } 

    /** 
    * set to the first second of current day 
    */ 
    public function setDayStart() 
    { 
     return $this->setHour(0)->setMinute(0)->setSecond(0); 
    } 

    /** 
    * get the first second of current day 
    */ 
    public function getDayStart() 
    { 
     $clone = clone $this; 
     return $clone->setDayStart(); 
    } 

    /** 
    * get count of days between dates, ignores time values 
    */ 
    public function getDaysBetween($date) 
    { 
     // 86400 seconds/day = 24 hours/day * 60 minutes/hour * 60 seconds/minute 
     // rounding takes care of time changes 
     return round($date->getDayStart()->sub(
      $this->getDayStart() 
     )->toValue()/86400); 
    } 
} 
3
$cerimonia = new Zend_Date('your date here'); 
    $days = $cerimonia->sub(Zend_Date::now()); 
    $days = round($days/86400)+1; 
0

numero di giorni tra la data di registrazione (più tardi) e la data di acquisto (prima)

// $datePurchase instanceof Zend_Date 
// $dateRegistration instanceof Zend_Date 
if($datePurchase && $dateRegistration) { 
    $diff = $dateRegistration->sub($datePurchase)->toValue(); 
    $days = ceil($diff/60/60/24)+1; 
} 
0

Se $ data è un oggetto Zend_Date è possibile utilizzare il seguente:

if ($date->isEarlier(Zend_Date::now()->subDay(2)){ 
    [...] 
} 

o altre funzioni subXxx dell'oggetto Zend_Date.