DateTime Sınıfının getTimestamp Metodu

getTimestamp metodu, DateTime sınıfının bir nesnesi için Unix timestamp'ini döndürür. Bu, 1 Ocak 1970 00:00:00 UTC'den belirtilen tarihe kadar geçen saniye sayısını temsil eden bir tamsayıdır. Metot parametre almaz.

Sözdizimi

php$datetime->getTimestamp();

Örnek

Mevcut zaman damgasını alalım:

php<?php
  $date = new DateTime();
  echo $date->getTimestamp();
?>

Kodun çalıştırma sonucu:

php1678901234

Örnek

Belirli bir tarih için timestamp alalım:

php<?php
  $date = new DateTime('2023-01-15 12:00:00');
  echo $date->getTimestamp();
?>

Kodun çalıştırma sonucu:

php1673784000

Örnek

İki tarihi timestamp'leri üzerinden karşılaştıralım:

php<?php
  $date1 = new DateTime('2023-01-01');
  $date2 = new DateTime('2023-01-15');
  
  if ($date1->getTimestamp() < $date2->getTimestamp()) {
    echo 'Date1 is earlier than Date2';
  }
?>

Kodun çalıştırma sonucu:

php'Date1 is earlier than Date2'

Ayrıca Bakınız

  • format metodu,
    tarihi verilen şablona göre biçimlendirir
  • time fonksiyonu,
    mevcut Unix zaman damgasını döndürür
  • modify metodu,
    DateTime nesnesinin tarihini değiştirir