PHP | date_timestamp_get() Function

PHP date_timestamp_get() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the date_timestamp_get() Function in PHP.

The built-in date_timestamp_get() function in PHP is an alias of the DateTime::getTimestamp(). This PHP function accepts DateTime object as a parameter and returns the Unix timestamp for the given object.

Syntax

Following below is the syntax to use this function -

date_timestamp_get(object)


Parameter Details

Sr.NoParameter & Description
1

object(Mandatory)

This is a DateTime object for which you need the time stamp for.


Return Value

This built-in function returns a Unix timestamp representing the given date.

PHP Version

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

Example1

Following example demonstrates the usage of date_timestamp_get() function -

<?php
   $date = date_create("25-09-1989, 07:32:41 GMT");
   $timestamp = date_timestamp_get($date);
   print("Timestamp: ".$timestamp);
?>

Output

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

Timestamp: 622711961

Example2

The following example retrieves the timestamp of current time -

<?php
   $date = date_create();
   $timestamp = date_timestamp_get($date);
   print("Timestamp: ".$timestamp);
?>

Output

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

Timestamp: 1606325502

Example3

The following example creates a date, adds an interval to it and then retrieves the time stamp of the resultant date -

<?php
   //Creating a DateTime object
   $date = date_create("25-09-1989");
   //Adding interval to the date
   $new_date = date_add($date, new DateInterval('PT10H30S'));     
   $timestamp = date_timestamp_get($new_date);      
   print("Timestamp: $timestamp");
?>

Output

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

Timestamp: 622720830

Example4

If you try the get the timestamp of the date before the epoch, then the date_create() function return a negative value -

<?php
   $date = date_create("1952-04-27");
   $timestamp = date_timestamp_get($date);  
   print($timestamp);   
?>

Output

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

-557971200


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

Do feel free to ask your questions where necessary and i 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.