How to get the current timestamp in PHP

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

Working with dates and times is a common task in many PHP applications. Whether you’re logging events, scheduling tasks, or setting timers, knowing how to accurately and efficiently retrieve the current timestamp is fundamental.

Obtaining the Current Timestamp

To simply retrieve the current Unix timestamp in PHP, you can use the time() function. This returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

$currentTimestamp = time();
echo $currentTimestamp;

Using the DateTime Class

A more object-oriented approach for managing dates and times is by using the DateTime class. You can get the current timestamp by creating a new instance of DateTime, then using the getTimestamp() method.

$date = new DateTime();
$currentTimestamp = $date->getTimestamp();
echo $currentTimestamp;

Formatting Your Timestamp

If you need a formatted string instead of a Unix timestamp, you can use the date() function. Specify the format you want as the first parameter. Here is how to get the current date and time in a human-readable form:

echo date('Y-m-d H:i:s');

Working with Timezones

In PHP, you can set the default timezone for all date/time functions with date_default_timezone_set(). This affects functions like date() and the DateTime object.

date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s');

For a DateTime object, you can specify the timezone upon instantiation.

$timezone = new DateTimeZone('Europe/Paris');
$date = new DateTime('now', $timezone);
echo $date->format('Y-m-d H:i:s');

Timestamp with Microseconds

For applications that require more precision, PHP offers the microtime() function to retrieve the current timestamp with microseconds.

$timestampWithMicroseconds = microtime(true);
echo $timestampWithMicroseconds;

Using the strtotime Function

The strtotime() function is invaluable when working with string representations of dates and times. It converts a string into a Unix timestamp.

echo strtotime('now');

Cache Control with Timestamps

Timestamps can be used to control caching mechanisms. For example, appending a timestamp query parameter to the URL of a JavaScript or CSS file can prevent browsers from loading old, cached versions after updates.

echo '';

Timestamps in Database Operations

In database operations, timestamps allow you to track changes and maintain records of when data was inserted, updated or deleted. Examples using PDO and MySQLi are shown here:

// With PDO
$pdo->prepare('INSERT INTO table (column, created_at) VALUES (?, ?)')->execute(['value', date('Y-m-d H:i:s')]);

// With MySQLi
$query = 'INSERT INTO table (column, created_at) VALUES (?, ?)';
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss', $value, date('Y-m-d H:i:s'));
$stmt->execute();

Performance Concerns

When dealing with high-load applications, it’s important to consider the performance implications of date/time functions. Caching timestamps or using built-in database functions can improve application throughput.

Conclusion

The ability to accurately obtain the current timestamp in PHP is essential for many applications. From the basic time() function to the object-oriented approach of the DateTime class, PHP offers various ways to get timestamps according to the needs of your project. With the knowledge of these methods, you’re well-equipped to handle any time-related functionality in your PHP applications.