English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Manual

PHP date_timestamp_get() Function Usage and Examples

PHP Date & Time Functions Manual

The date_timestamp_get() function gets the Unix timestamp

Definition and Usage

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

Syntax

date_timestamp_get(object)

Parameter

NumberParameter and Description
1

object (required)

This is the DateTime object for which you need the timestamp.

Return Value

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

PHP Version

This function was originally introduced in PHP version5.3introduced in PHP 5.2 and is available in all later versions.

Online Example

The following examples demonstratedate_timestamp_get()The function returns the timestamp of the specified date

<?php
   $date = date_create("1985-12-19, 07:32:41 GMT
   $timestamp = date_timestamp_get($date);
   print("Timestamp: $timestamp");
?>
Test to see‹/›

Output Result

Timestamp: 503825561

Online Example

The following example returns the Unix timestamp of today's date and time:

<?php
   $date = date_create();
   $timestamp = date_timestamp_get($date);
   print("Timestamp: $timestamp");
?>
Test to see‹/›

Output Result

Timestamp: 1589179558

Online Example

The following example creates a date, adds an interval to it, and gets the timestamp of the resulting date-

<?php
   //Create a DateTime object
   $date = date_create("25-09-1989");
   //Add an interval to the date
   $new_date = date_add($date, new DateInterval('PT10H30S'));     
   $timestamp = date_timestamp_get($new_date);      
   print("Timestamp: $timestamp");
?>
Test to see‹/›

Output Result

Timestamp: 622720830

Online Example

If you try to get the timestamp of a date before the Unix epoch date (1970 year1month1If the day is), thendate_create()The function will return a negative value-

<?php
   $date = date_create("1952-04-27");
   $timestamp = date_timestamp_get($date);  
   print($timestamp);   
?>
Test to see‹/›

This produces the following result-

-557971200