Getting a timestamp for today at midnight?

Getting a timestamp for today at midnight?

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

Stefan Bogdanescu

Founder & Senior Architect

Getting a timestamp for today at midnight?

When you need to calculate a specific point in time, such as "today at midnight," relying on simple string manipulation or guessing offsets is a recipe for errors. The core challenge lies in handling the transition between days correctly within your server's timezone context. If your application operates in UTC while your users expect local midnight, mixing these two contexts without explicit conversion will result in incorrect timestamps that drift as you cross time zone boundaries.

In PHP, the most robust approach is to utilize the DateTime class combined with DateTimeZone. This allows you to define a specific moment in time relative to the current date in a specific timezone, rather than relying on the raw Unix timestamp of "now" which includes the seconds and minutes that have already passed.

Here is a practical pattern to achieve this:

$timezone = new DateTimeZone('America/New_York'); // Replace with your specific zone
$todayAtMidnight = new DateTime('tomorrow midnight', $timezone);

// Subtract one day to get today's midnight, or simply set the time components
$now = new DateTime('now', $timezone);
$midnight = clone $now;
$midnight->modify('-1 hour'); // Adjust based on timezone offset logic if strictly needed

Wait, the previous snippet is overly complicated. Let's refine the logic to be cleaner and less error-prone. The standard DateTime constructor accepts a string format like 'tomorrow midnight', but that gives you the next day's start. To get today's midnight (which has already passed if it is currently after 12:00 AM), you must subtract one day from tomorrow's midnight, or more simply, set the time components directly on a current date object.

A cleaner implementation looks like this:

$timezone = new DateTimeZone('UTC'); // Or your local timezone
$todayMidnight = new DateTime();
$todayMidnight->setTime(0, 0, 0);
$todayMidnight->modify('-1 day'); // Adjust based on whether you want today or yesterday

// To strictly get "today" (even if it is currently 5 PM), we don't modify the date.
// We just zero out the time and keep the current date.
$targetDate = new DateTime(); 
$targetDate->setTime(0, 0, 0); 

if ($targetDate < $now) {
    // It is already past midnight today? No, if we just set time to 0:00 on current date,
    // and it is currently 5 PM, then $targetDate represents "today at midnight".
}

echo $targetDate->format('U'); // Returns the Unix timestamp for today at 00:00:00

Actually, let's simplify that logic flow. If you instantiate a DateTime object with the current date and set its time to 00:00:00, PHP automatically handles the timezone conversion if you initialize it correctly. However, there is a subtle pitfall: simply calling new DateTime() creates an object in UTC by default unless configured otherwise. If your server is in EST (UTC-5), midnight in EST is 5 AM UTC. If you format that timestamp directly without awareness of the offset, you might misinterpret it.

The safest, most direct method to get the timestamp for "today at local midnight" is:

$timezone = new DateTimeZone('America/New_York'); // Set your target timezone
$midnight = new DateTime('tomorrow midnight', $timezone);
$midnight->modify('-1 day');

// This moves us back one day from tomorrow's start to today's start.
echo $midnight->format('U'); 

This approach avoids complex setTime calculations that can sometimes confuse the date logic if not handled carefully regarding leap seconds or DST transitions, although PHP handles DST well. The key is ensuring the DateTimeZone matches the definition of "today" you require. If a user in London logs in at 5 PM local time on Monday, and you want their "Monday midnight," you must calculate based on their timezone, not the server's.

In Laravel, this pattern translates easily using Carbon, which wraps these concepts for better readability:

use Illuminate\Support\Carbon;

$midnight = Carbon::now('America/New_York')->startOfDay()->subDay();
// Or more simply if you just want today's midnight regardless of current time:
$todayMidnight = Carbon::today('America/New_York')->startOfDay(); 
// Wait, startOfDay on "today" returns the next midnight if it is currently past midnight? 
// No, Carbon::today() resets to 00:00 of the current date.

Correction: Carbon::today() creates an instance for today at 00:00. If you need the timestamp value for that moment, ->timestamp or ->getTimestamp() provides the Unix integer. The critical distinction is whether "today" refers to the calendar date in UTC or the local timezone. If you are building a billing system where a day rolls over at midnight in Paris but not New York, hardcoding the timezone is mandatory.

The primary pitfall here is assuming the server's date_default_timezone is sufficient. While setting date_default_timezone_set() works for procedural code, object-oriented approaches like DateTimeZone are explicit and prevent silent bugs during deployment to new environments. Always specify the zone when constructing the time object.

Our network