PHP | date_timestamp_set() Function

The date_timestamp_set() function is an inbuilt function in PHP which is used to sets the date and time based on an Unix timestamp. This function returns the DateTime object for method chaining or False on failure.

Syntax:

  • Procedural style:
    date_timestamp_set( $object, $unixtimestamp )
  • Object oriented style:
    DateTime::setTimestamp( $unixtimestamp )

Parameters: This function accepts two parameters as mentioned above and described below:



  • $object: It is a mandatory parameter which is used to specify the DateTime object which is returned by the date_create() function.
  • $unixtimestamp: This parameter is used to set the Unix timestamp representing the date.

Return Value: This function returns the DateTime object on success or False on failure.

Below programs illustrate the date_timestamp_set() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create DateTime object
$date = date_create();
  
// Display DateTime in given format
echo date_format($date, 'U = d-m-Y H:i:s') . "\n";
  
// date_timestamp_set function to Set
// Unix timestamp
date_timestamp_set($date, 1373502124);
  
// Display DateTime in given format
echo date_format($date, 'U = d-m-Y H:i:s');
?>

chevron_right


Output:

1537159667 = 17-09-2018 04:47:47
1373502124 = 11-07-2013 00:22:04

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create DateTime object
$date = new DateTime();
  
// Display DateTime in given format
echo $date->format('U = d-m-Y H:i:s') . "\n";
   
// date_timestamp_set function to Set
// Unix timestamp
$date->setTimestamp(1171564674);
  
// Display DateTime in given format
echo $date->format('U = d-m-Y H:i:s');
?>

chevron_right


Output:

1537159667 = 17-09-2018 04:47:47
1171564674 = 15-02-2007 18:37:54

Related Articles:

Reference: http://php.net/manual/en/datetime.settimestamp.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.