Given a Unix timestamp, how to get beginning and end of that day?

Given a Unix timestamp, how to get beginning and end of that day?

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

Stefan Bogdanescu

Founder & Senior Architect

Calculating Day Boundaries from Unix Timestamps

Working with Unix timestamps is a common requirement in backend development, but deriving calendar-aligned boundaries often trips up developers who assume the math is linear and constant. If you have a timestamp like 1330581600 and need to determine the exact start and end of that specific day, simply adding or subtracting seconds based on an average day length (86,400 seconds) can introduce subtle errors if your application operates in a timezone where Daylight Saving Time is active.

The core challenge lies in the definition of "a day." In Unix time, a day is not a fixed block of 86,400 seconds; it is a local calendar concept that shifts depending on your server's configuration. When you ask for the beginning and end of the day for a specific timestamp, you are essentially asking: "What are the UTC boundaries of the local date represented by this timestamp?"

The Timezone Problem

Your initial approach—$endOfDay = $timestamp + (60 * 60 * 23);—fails because it assumes the target day ends exactly 23 hours after the input. This logic only holds true if:

  1. You are working strictly in UTC, where every day is exactly 86,400 seconds.
  2. No Daylight Saving Time (DST) transitions occur on that specific day.

If your server is configured to use a timezone like America/New_York, and the timestamp falls on a Sunday where clocks spring forward at 2:00 AM, the actual duration of that day in Unix seconds is only 86,340. Conversely, if DST ends, the day lasts 86,460 seconds. Hardcoding hour calculations ignores this reality.

To get accurate boundaries, you must first convert the timestamp into a localized datetime object. This allows you to anchor your calculations to the specific calendar date in that timezone, regardless of how many actual seconds passed between midnight and the next midnight.

Implementation in PHP

The most robust way to solve this is by utilizing DateTime objects. You create an instance from the Unix timestamp while explicitly setting the desired timezone. Once you have a valid DateTime object representing the start of your target day, you can manipulate it to find the boundaries.

Here is how you would calculate both values in PHP:

$timestamp = 1330581600;
$timezone = new DateTimeZone('America/New_York'); // Or whatever your app uses

// Create a DateTime object for the exact moment of the timestamp
$dt = new DateTime('@' . $timestamp, $timezone);

// Get the formatted date string (e.g., "2012-03-01") to anchor to midnight
$dateString = $dt->format('Y-m-d');

// Define the beginning of the day at 00:00:00 in that timezone
$beginOfDay = new DateTime($dateString, $timezone);
$beginOfDay->setTime(0, 0, 0);

// Define the end of the day at 23:59:59.999999 in that timezone
// We set it to 24:00:00 and subtract a microsecond to ensure we stay within the day's local time
$endOfDay = new DateTime($dateString, $timezone);
$endOfDay->modify('+1 day');
$endOfDay->setTime(0, 0, 0, -1); // Or simply use format('Y-m-d 23:59:59')

// If you need these as Unix timestamps again
$beginTimestamp = $beginOfDay->getTimestamp();
$endTimestamp = $endOfDay->getTimestamp();

echo "Start: {$beginTimestamp}";
echo "End:   {$endTimestamp}";

Why This Approach Works

By creating a new DateTime object based on the date string derived from the original timestamp, you effectively reset the time to midnight. This operation respects the local timezone rules. The resulting $beginOfDay object represents the exact Unix timestamp for 00:00:00 of that day in your specific timezone.

For the end of the day, it is safer to calculate the start of the next day and subtract one second, or explicitly set the time to 23:59:59. The code snippet above demonstrates setting the next day's midnight as a reference point, which guarantees you have captured the full local duration of the target date.

Trade-offs and Considerations

When implementing this logic, consider whether your downstream systems expect UTC or local timestamps. If you store these boundaries in a database, storing them as DateTime objects with timezone awareness is preferable to storing raw Unix integers without context. However, if you must persist Unix timestamps, ensure the application consistently interprets them according to the same timezone rules used during calculation.

If you are working within the Laravel ecosystem, similar logic applies using Carbon. You can pass a timestamp and a timezone to Carbon, then use the startOfDay() and endOfDay() modifiers which inherently handle DST shifts correctly without manual math. For instance, Carbon::createFromTimestamp($timestamp, 'America/New_York')->startOfDay()->getTimestamp() returns the UTC equivalent of midnight in that timezone.

Accuracy here prevents data leakage across dates in reporting tools and ensures event logs group correctly by calendar day rather than by raw epoch duration.

Our network