getTimestamp() : Timestamp from Date object

Get a Unix timestamp from a date object

getTimestamp() returns the Unix timestamp represented by a DateTime or DateTimeImmutable object.

$date = new DateTimeImmutable('2026-09-14 12:00:00', new DateTimeZone('UTC'));
echo $date->getTimestamp();

The timestamp is timezone-independent; changing the object's timezone changes its formatted clock representation, not the represented instant.

getTimestamp ($unixtimestamp);
date_timestamp_set ($date_obj,$unixtimestamp);
Gets the Unix timestamp of the date.
ParameterDESCRIPTION
$date_objRequired : Input date object to be assigned new value.
$unixtimestampRequired: Integer , Timestamp of new date and time
mktime() function to generate timestamp
Generate timestamp using date and time

Object Oriented style

Current timestamp
$date = new DateTime(); // current time object
echo "Timestamp: ".$date->getTimestamp()."<br>"; // getting Timestamp
echo $date->format('U = Y-m-d H:i:s');

Procedural style

Current timestamp
$date = date_create(); // current time object
echo "Timestamp: ".date_timestamp_get($date)."<br>"; // getting Timestamp
echo date_format($date, 'U = Y-m-d H:i:s');

Timestamp of any date and time ( Object Oriented Style )

$date = new DateTime('2016-08-25 22:15:40');
echo "Timestamp: ".$date->getTimestamp();// Output is Timestamp: 1472143540

Timestamp of any date and time ( Procedural Style )

$date = date_create('2019-12-28 22:15:40');
echo "Timestamp: ".date_timestamp_get($date); // Output is Timestamp: 1577551540

Other ways to get Unix Timestamp

Function Description
mktime() getting time stamp of input date and time
strtotime()Text string of data to timestamp
PHP Date Functions setDate() : Set date for date object Timestamp in PHP Displaying calendar for selection of date by user


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer