Get timestamp of today and yesterday in php

Get timestamp of today and yesterday in php

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Variable Timestamps for Today, Yesterday, and Day Before Yesterday in PHP

Determining specific timestamps for relative dates is a common requirement when building scheduling systems or logging mechanisms. While strtotime() is often the go-to function for this task, relying on it blindly can lead to subtle bugs if the logic isn't constructed carefully. Specifically, capturing "12 o'clock" requires treating that time as a variable parameter rather than a hardcoded constant. If your application needs to handle user-defined times—say, 12:00 PM versus 12:00 AM—you must ensure your calculation logic remains flexible enough to accept external input without breaking the date arithmetic.

The core challenge lies in separating the date component from the time component. When you request a timestamp for "today," you are essentially asking for the Unix timestamp of a specific moment on the current calendar day. If you simply append "+12:00" to today's midnight, you might inadvertently skip the actual 12 o'clock window if the user inputs a value that falls after the current time has already passed. For instance, if it is currently 3:00 PM and a user specifies "12 o'clock," a naive script should not target tomorrow at noon unless explicitly told to look forward; however, in many business contexts, "today" implies the current calendar day regardless of whether that specific hour has occurred yet.

To handle this robustly, you need a strategy that calculates the start of the desired day and then offsets it by the user-provided hours. The strtotime() function is excellent for parsing relative strings like "now", "+1 day", or "-2 days". You can combine this with date() to construct a string representing midnight on the target date, which you can then pass back to strtotime to apply the time offset.

Here is a practical implementation pattern that accepts a variable hour as input:

function getTimestampForTime($targetDateOffset, $hour) {
    // Define the start of the day (midnight) for the target date relative to now
    // 'today', '+1 day', etc., are passed here
    $startOfDay = strtotime("00:00 {$targetDateOffset}");
    
    // Add the desired hours to the start of the day
    $timestamp = strtotime("+$hour hours", $startOfDay);
    
    return $timestamp;
}

// Example usage for today at 12 o'clock (user defined as 12)
$userHour = 12; 
$todayTimestamp = getTimestampForTime('today', $userHour);

This approach isolates the date calculation from the time manipulation. By first establishing $startOfDay using a fixed "00:00" offset, you create a reliable baseline. The second strtotime() call then applies the user's variable hour ($hour) to that baseline. This prevents errors where adding hours to "now" might accidentally push the timestamp into next day if the current time is late in the evening.

Pitfalls with Relative Date Strings

One common mistake developers make is assuming that string concatenation always works as expected with strtotime(). While strtotime() is powerful, it can be finicky with mixed formats. For example, passing a timestamp directly to date('Y-m-d H:i:s', $timestamp) is generally safe, but generating the input string for strtotime requires attention to detail.

If you need to store these timestamps in a database, ensure your column type matches the Unix integer format (usually BIGINT) or use a DATETIME column if you prefer storing native PHP date strings. If you are using Laravel, consider using Carbon, which handles these relative calculations with less ambiguity:

use Carbon\Carbon;

$userHour = 12;
$todayAtUserHour = Carbon::today()->hour($userHour)->toDateTime();

Carbon's hour() method is particularly clean for this specific use case because it treats the date object as immutable until modified, ensuring that changing the hour doesn't affect the underlying date calculation logic unexpectedly. This trade-off adds a slight dependency but significantly reduces the cognitive load of parsing relative strings manually.

When integrating these features into larger systems, always validate that the resulting timestamp falls within your expected range. If your application operates across time zones, remember to convert the target timestamp to UTC for storage and only convert it back to the local user timezone for display. This ensures consistency regardless of where the server is hosted or what the user's local clock says.

Our network