0
0
PhpHow-ToBeginner · 3 min read

How to Get Timestamp in PHP: Simple Guide

In PHP, you can get the current timestamp using the time() function, which returns the number of seconds since January 1, 1970. Alternatively, you can use the DateTime class and call getTimestamp() for more flexibility.
📐

Syntax

The simplest way to get the current timestamp is using the time() function. It returns an integer representing seconds since the Unix Epoch (January 1, 1970).

Another way is to create a DateTime object and call its getTimestamp() method to get the timestamp.

php
<?php
// Using time() function
$timestamp = time();

// Using DateTime class
$date = new DateTime();
$timestampFromDateTime = $date->getTimestamp();
?>
💻

Example

This example shows how to get the current timestamp using both time() and DateTime. It prints the timestamp and a formatted date for clarity.

php
<?php
// Get timestamp using time()
$timestamp = time();
echo "Timestamp using time(): $timestamp\n";

// Get timestamp using DateTime
$date = new DateTime();
$timestampFromDateTime = $date->getTimestamp();
echo "Timestamp using DateTime: $timestampFromDateTime\n";

// Show formatted date
echo "Formatted date: " . date('Y-m-d H:i:s', $timestamp) . "\n";
?>
Output
Timestamp using time(): 1718827200 Timestamp using DateTime: 1718827200 Formatted date: 2024-06-20 12:00:00
⚠️

Common Pitfalls

One common mistake is expecting time() to return milliseconds or microseconds; it only returns seconds. For higher precision, use microtime(true).

Another pitfall is forgetting to set the correct timezone when using DateTime, which can cause unexpected timestamp values.

php
<?php
// Wrong: expecting milliseconds from time()
$wrongTimestamp = time();
echo "Wrong timestamp (milliseconds expected): $wrongTimestamp\n";

// Right: use microtime(true) for float seconds with microseconds
$correctTimestamp = microtime(true);
echo "Correct timestamp with microseconds: $correctTimestamp\n";

// Wrong: DateTime without timezone (may default to server timezone)
$date = new DateTime('now');
echo "DateTime timestamp (default timezone): " . $date->getTimestamp() . "\n";

// Right: specify timezone explicitly
$dateWithTZ = new DateTime('now', new DateTimeZone('UTC'));
echo "DateTime timestamp (UTC timezone): " . $dateWithTZ->getTimestamp() . "\n";
?>
Output
Wrong timestamp (milliseconds expected): 1718827200 Correct timestamp with microseconds: 1718827200.123456 DateTime timestamp (default timezone): 1718827200 DateTime timestamp (UTC timezone): 1718827200
📊

Quick Reference

  • time(): Returns current Unix timestamp in seconds (integer).
  • microtime(true): Returns current Unix timestamp with microseconds as float.
  • DateTime::getTimestamp(): Returns timestamp from a DateTime object.
  • Always check or set timezone when using DateTime to avoid confusion.

Key Takeaways

Use time() to get the current timestamp in seconds since 1970.
For microsecond precision, use microtime(true) instead of time().
Use DateTime and getTimestamp() for flexible date and time handling.
Always set or check the timezone when working with DateTime to avoid errors.
Remember time() returns seconds, not milliseconds.