We now have a youtube channel. Subscribe!

PHP | date_timestamp_set() Function

PHP date_timestamp_set() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_timestamp_set() Function.

The PHP date_timestamp_set() function is an alias of the DateTime::setTimestamp(). This PHP function accepts DateTime object and an Unix timestamp as parameters and sets the specified timestamp to the given object.

Syntax

Following below is the syntax to use this function -

date_timestamp_set($object, $timestamp)


Parameter Details

Sr.NoParameter & Description
1

object(Mandatory)

This is a DateTime object.

2

timestamp(Mandatory)

This is an Unix timestamp .


Return Value

This function returns the DateTime object with modified time value. In case of failure, this PHP function returns the boolean value false.

PHP Version

This function was first introduced as part of core PHP version 5.3.0 and, it works with all of the later versions.

Example1

Following example demonstrates the usage of date_timestamp_set() function -

<?php
   $date = new DateTime();
   $res = date_timestamp_set($date, 1505292545);   
   print("Date: ".date_format($res, "Y/m/d H:i:s"));
?>

Output

When the above code is executed, it will produce the following result -

Date: 2017/09/13 08:49:05

Example2

The following example creates a DateTime object and modifies its time value using the built-in PHP date_timestamp_set() Function -

<?php
   $date = new DateTime();
   $timestamp1 = time() - (23*12*30);   
   $res1 = date_timestamp_set($date, $timestamp1);      
   print("Date: ".date_format($res1, "Y/m/d H:i:s"));
   print("\n");
   $timestamp2 = time() + (23*12*30);
   $res2 = date_timestamp_set($date, $timestamp2);  
   print("Date: ".date_format($res2, "Y/m/d H:i:s"));
?>

Output

When the above code is executed, it will produce the following result -

Date: 2020/05/11 08:57:30
Date: 2020/05/11 13:33:30

Example3

As an alternative to this function you can just pass the time stamp value as a string preceded by "@" as a parameter to the DateTime construct -

<?php
  $date = new DateTime("@1495283256");
   print("Date: ".date_format($date, "Y/m/d H:i:s"));?>

Output

When the above code is executed, it will produce the following result -

Date: 2017/05/20 12:27:36


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the date_get_last_errors() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain