DateTime Sınıfının setTimestamp Metodu

DateTime nesnesinin setTimestamp metodu, tarih ve saati Unix timestamp (1 Ocak 1970'den itibaren geçen saniye sayısı) kullanarak ayarlamayı sağlar. Metot mevcut tarih nesnesini değiştirir ve zincirleme çağrılar için aynı nesneyi döndürür.

Sözdizimi

phppublic DateTime::setTimestamp(int $timestamp): DateTime

Örnek

1609459200 timestamp değerine karşılık gelen tarihi ayarlayalım (1 Ocak 2021 00:00:00 UTC):

php<?php
  $date = new DateTime();
  $date->setTimestamp(1609459200);
  echo $date->format('Y-m-d H:i:s');
?>

Kodun çalıştırılma sonucu:

php'2021-01-01 00:00:00'

Örnek

Metot zincirlerinde kullanım:

php<?php
  $date = (new DateTime())->setTimestamp(1609459200);
  echo $date->format('Y-m-d');
?>

Kodun çalıştırılma sonucu:

php'2021-01-01'

Örnek

Mevcut zamanı ayarlama:

php<?php
  $date = new DateTime();
  $date->setTimestamp(time());
  echo $date->format('H:i:s');
?>

Mevcut zamanı gösterecektir, örneğin:

php'14:25:36'

Ayrıca Bakınız

  • Unix timestamp'ı biçimlendiren date
    fonksiyonu
  • Tarihi parçalar halinde ayarlayan setDate
    metodu
  • DateTime nesnesinden Unix timestamp alan getTimestamp
    metodu