Converting TIMESTAMP to unix time in PHP?

Converting TIMESTAMP to unix time in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting TIMESTAMP to Unix Time in PHP

Storing dates as human-readable strings like 2010-05-17 19:13:37 is a common pattern in legacy databases and some modern applications. While this format is intuitive for end-users, it introduces complexity when performing calculations or comparisons within your application logic. Comparing string representations of dates often leads to errors if the database collation settings aren't perfectly aligned with your expectations, or if you need to perform arithmetic operations that require numeric values.

Converting these datetime strings into Unix timestamps—integer values representing the number of seconds elapsed since January 1, 1970, 00:00:00 UTC—is a standard practice for simplifying time logic in PHP. The solution is straightforward and relies on the built-in strtotime() function or the more explicit DateTime class.

If your data resides in a MySQL column formatted as '2010-05-17 19:13:37', you can retrieve it from the database and pass that string directly to strtotime(). This function attempts to parse various date formats and returns the corresponding Unix timestamp.

$dbTime = '2010-05-17 19:13:37';
$unixTime = strtotime($dbTime);
// $unixTime will be 1274119041

While strtotime() is convenient for one-off conversions or parsing unpredictable input, relying on it exclusively for database-driven logic can sometimes introduce ambiguity. Specifically, strtotime() interprets dates based on the server's default timezone unless specified otherwise. If your application runs in a different timezone than UTC, you might get incorrect results if the database was populated with timestamps assuming UTC.

For production applications where precision matters, the DateTime class is the preferred approach. It offers explicit control over timezones and avoids the "magic" behavior of string parsing functions. To convert a standard datetime string to a Unix timestamp using DateTime, you can construct an object and call its getTimestamp() method:

$dbTime = '2010-05-17 19:13:37';
$date = new DateTime($dbTime);
$unixTime = $date->getTimestamp();

This approach is generally safer because it treats the input as a string to be parsed into an object, rather than trying to guess the intent behind the string format. It also allows you to immediately set the timezone if needed before generating the timestamp.

When comparing two times in PHP, using Unix timestamps eliminates the need to worry about date formats or collation rules. You can simply subtract one timestamp from another to get the difference in seconds:

$timeA = strtotime('2010-05-17 19:13:37');
$timeB = strtotime('2010-05-17 20:00:00');

$diffInSeconds = $timeB - $timeA; // Result: 2460 seconds

If you are building a Laravel application, the framework abstracts much of this complexity away from raw SQL. In Laravel, you can use Carbon, which extends PHP's DateTime class, to handle these conversions seamlessly. Carbon automatically handles timezone conversions and parsing, making it significantly more robust than native PHP functions when dealing with real-world edge cases like daylight saving time transitions or ambiguous timestamps.

use Illuminate\Support\Carbon;

// Assuming $record is your model instance
$timestamp = Carbon::parse($record->created_at)->getTimestamp();

The choice between strtotime(), the native DateTime class, and Carbon often comes down to the context of your application. For simple scripts or legacy codebases where performance is critical and inputs are controlled, strtotime() works fine. However, for any new feature touching time data, the DateTime class or Laravel's Carbon provides the necessary safety nets to prevent subtle bugs caused by timezone mismatches or format ambiguity.

Our network