PHP | date_timestamp_get() Function

PHP date_timestamp_get() 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_get() Function.

The PHP date_timestamp_get() function is an alias of DateTime::getTimestamp(). This PHP function accepts DateTime object as 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 back 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

The 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

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 to get the timestamp of the date before epoch, then the PHP date_create() function will 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 going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the date_timestamp_set() 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.