• PHP Video Tutorials

PHP date_timestamp_get() Function


Advertisements

Definition and Usage

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

Syntax

date_timestamp_get(object)

Parameters

Sr.No Parameter & Description
1

object(Mandatory)

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

Return Values

PHP date_timestamp_get() function returns an Unix timestamp representing the given date.

PHP Version

This function was first introduced in PHP Version 5.3 and, works with all the later versions.

Example

Following example demonstrates the usage of the date_timestamp_get() function −

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

This will produce following result −

Timestamp: 622711961

Example

Following example retrieves the time stamp of current time −

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

This will produce following result −

Timestamp: 1589179558

Example

Following example creates a date adds an interval to it and retrieves the timestamp of the resultant date −

Live Demo
<?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");
?>

This will produce following result −

Timestamp: 622720830

Example

If you try to get the timestamp of the date before unix epoch date (1 January 1970) the date_create() function returns a negative value −

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

This produces the following result −

-557971200
php_function_reference.htm
Advertisements