PHP Function Reference

PHP date_timestamp_set() Function



The PHP date_timestamp_set() function sets the date and time a DateTime object, based on a specified Unix timestamp. This function is an alias of DateTime::setTimestamp() method.

Note: Unix timestamp indicates the number of seconds since midnight of January 1, 1970 (Gregorian Calendar).

Syntax

//Object-oriented style
public DateTime::setTimestamp(timestamp)

//Procedural style
date_timestamp_set(object, timestamp)

Parameters

object Required. For procedural style only: A DateTime object returned by date_create().
timestamp Required. Specify a Unix timestamp representing the date.

Return Value

Returns the DateTime object with modified date and time on success, otherwise returns false.

Example: using both styles

The example below shows the usage of date_timestamp_set() function.

<?php
  //creating a DateTime object
  $date = date_create();

  //setting date and time using Object-oriented style
  $date->setTimestamp(1307297845); 

  //formatting the datetime to print it
  echo date_format($date, "d-M-Y H:i:s")."\n";

  //setting date and time using Procedural style
  date_timestamp_set($date, 1500292845); 

  //formatting the datetime to print it
  echo date_format($date, "d-M-Y H:i:s")."\n";
?>

The output of the above code will be:

05-Jun-2011 18:17:25
17-Jul-2017 12:00:45

Example: Modifying date and time of a DateTime object

This function can be used to modify date and time of the DateTime object to a new value. Consider the example below:

<?php
  //datetime string
  $datetime_string = "14-May-2015 5:10";

  //creating a DateTime object
  $date = date_create($datetime_string);

  //formatting the datetime to print it
  echo "Original DateTime: ".date_format($date, "d-M-Y H:i:s")."\n";

  //setting date and time to a new value
  date_timestamp_set($date, 1500292845); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".date_format($date, "d-M-Y H:i:s")."\n";
?>

The output of the above code will be:

Original DateTime: 14-May-2015 05:10:00
Modified DateTime: 17-Jul-2017 12:00:45

❮ PHP Date and Time Reference