DateTime :: diff () nie ma wpływu na strefę czasową, ale zawsze zwraca tę samą wartość
Dodaję do systemu dane oparte na aukcjach. Na przykład:
user's timezone is : `America/Denver`
auction start time is : Thu, 21 Jun 2012 03:46:22 AM
auction period : 1 day.
auction end time : Fri, 22 Jun 2012 03:46:22 AM
Przechowuję wszystkie te informacje tak, jak są w moim DB (tj.cały czas przechowywany na podstawie strefy czasowej użytkownika).
Teraz chcę rzeczywistegoróżnica między czasem końcowym a czasem bieżącym (strefa czasowa America / Denver).
użyłemDateTime()
Funkcje
więc nawet ja nie konwertuję strefy czasowej; zwraca tę samą różnicę (i to też jest złe).
Mój kod PHP jest poniżej; możesz go również znaleźć wCodePad
$end_time = 'Fri, 22 Jun 2012 03:46:22 AM';
$tz = 'America/Denver';
dateDifference($end_time,$tz);
function dateDifference($end, $tz = NULL)
{
$owner_tz = new DateTimeZone($tz);
//var_dump($owner_tz->getName());
$from = new DateTime("now", new DateTimeZone('GMT'));
$from ->setTimeZone($owner_tz);
$to = new DateTime($end,new DateTimeZone('GMT'));
/* @Note:I have commented below setTimeZone() because I have already stored
* `end time` as per user's timezone, So I dont need to convert it again
* into user's timezone.you can un-comment below line.
* it doesn't affect the results anyways.
*/
//$to ->setTimeZone($owner_tz);
//procedural style using date_diff()
$interval = date_diff($from, $to);
var_dump($interval);
// OO style using dateTime::diff()
$interval = $from->diff($to);
var_dump($interval);
}
Zwraca:
object(DateInterval)[4]
public 'y' => int 0
public 'm' => int 0
public 'd' => int 0
public 'h' => int 16
public 'i' => int 40
public 's' => int 24
public 'invert' => int 0
public 'days' => int 6015
Referencje:
DateTime()
DateTime::diff
DateTime::setTimeZone
Obsługa konwersji stref czasowych za pomocą PHP DateTime