149

How would I go about getting a timestamp in php for today at midnight. Say it's monday 5PM and I want the Timestamp for Monday(today) at midnight(12 am) which already has happened.

Thank you

6
  • Midnight is not am nor pm. Also technically, no timestamp for midnight exists.
    – hakre
    Oct 29, 2012 at 21:18
  • 23
    @hakre I disagree with you. Midnight is am and whatever format a timestamp is in, there is a value for midnight.
    – G-Nugget
    Oct 29, 2012 at 21:21
  • 2
    if 00:00:00 considered today or tomorrow. Midnight today, is behind us, at -almost- all times, technically. You want 23:59:59 of "tonight", or 00:00:00 "midnight of today, which is tomorrow", more or less.
    – wesside
    Oct 29, 2012 at 21:23
  • 5
    a time of 00:00:00 would be today's midnight as in however much time it takes to get to the 00:00:00 that already happened. where as 23:59:59 would be tonight. If your speaking in terms of today. Midnight is AM.
    – Nazca
    Jan 31, 2014 at 17:12
  • 6
    00:00 is midnight at the beginning of a day, 24:00 is midnight at the end of a day. For simplicity however, most digital clocks skip 24:00 - declaring that midnight is the start of a new day. I.e. 8th of Feb 24:00 and 9th of Feb 00:00 is in practice the same point in time.
    – Johan
    Sep 20, 2015 at 9:17

10 Answers 10

290
$timestamp = strtotime('today midnight');

or via a DateTime:

$date = new DateTime('today midnight');
// or: $date = date_create('today midnight');
$timestamp = $date->getTimestamp();

and then perhaps immutable:

$midnight = new DateTimeImmutable('today midnight');
// or: $midnight = date_create_immutable('today midnight');
$timestampOfMidnight = $midnight->getTimestamp();
  1. That is in the default timezone.
  2. Spoiler: just "midnight" or or just "today" return the same.
  3. Add a timezone, e.g. "UTC today", to have it, always.
  4. Spoiler #2: UTC greeting style: "midnightZ"
  5. History: midnight is since PHP 5.1.2 (Jan 2006), today since PHP 4.3.1 (Feb 2003)

More examples:

given the time UTC 2020-01-01 00:00:00:

UTC time is ............: [red   ] 1577836800

when calling strtotime($), results are:

today midnight .........: [pink  ] 1577833200
midnight ...............: [pink  ] 1577833200
today ..................: [pink  ] 1577833200
tomorrow ...............: [green ] 1577919600
UTC today ..............: [red   ] 1577836800
today Z ................: [red   ] 1577836800
Asia/Shanghai today ....: [lime  ] 1577808000
Asia/Shanghai ..........: [blue  ] 1577811600
HKST today .............: [copper] 1577804400

Online Demo: https://3v4l.org/KWFJl


PHP Documentation:

  • On the Relative Formats page, see the Day-based Notations table. These formats are for strtotime(), DateTime and date_create().

  • You might want to take a look what more PHP has to offer: https://php.net/datetime - the entry page to date-time related functions and objects in PHP with links to other, date-time related extensions.


NOTE: While "midnight" being technically between two days, here it is "today" (start of day) with PHPs' strtotime.

Discussion:

In so far, the answer strtotime("today midnight") or strtotime("midnight today") is a complete and well sounding answer for PHP, it may appear a bit verbose as strtotime("midnight") and strtotime("today") return the same result.

But even being more verbose not always instantly answers the question if it is about the Midnight for start of day or the Midnight for end of day even today is given as context. We may think about the start of day when reading "today midnight", but this is an assumption and not precise and perhaps can't be. Wikipedia:

Though there is no global unanimity on the issue, most often midnight is considered the start of a new day and is associated with the hour 00:00.

Compare with this programming question:

  • Given midnight is a time transition
  • When asking for a single UNIX timestamp
  • Then there is no answer

(it would be between two UNIX timestamps, so you would take two timestamps and describe what the two mean and this would not answer the question as it asks for a single timestamp).

This is not easy to completely resolve because of this mismatch and by how UNIX Time references date/time.

Lets take the well known, digital 24-hour clock with hours and minutes and express midnight (is it more precise?):

$midnight = strtotime("today 00:00");

or for end of day:

$midnight = strtotime("today 24:00");

(NOTE: shown as start of next day)

Or the 12-hour clock, it can also be used to give the UNIX Timestamp of today at midnight:

$midnight = strtotime("12:00 a.m.");

(NOTE: the "12 midnight" notation is not supported by strtotime())

The confusion that can result of a transition time like Midnight to map on a clock may even become more visible with the 12-hour clock as similar to "midnight" the midday (not available in PHP as a relative date/time format but "noon") is technically between again (now between two full dates at noon, or between the first and the second half of a day).

As this adds up, the code is likely not well received over a 24 hour clock or just writing out "today midnight".

Your mileage may vary.

This is kind of aligned with clock time. From Wikipedia Midnight:

"Midnight is the transition time from one day to the next – the moment when the date changes, on the local official clock time for any particular jurisdiction. By clock time, midnight is the opposite of noon, differing from it by 12 hours. [bold by me]"

and from the Wikipedia 12-hour clock:

"It is not always clear what times "12:00 a.m." and "12:00 p.m." denote. From the Latin words meridies (midday), ante (before) and post (after), the term ante meridiem (a.m.) means before midday and post meridiem (p.m.) means after midday. Since "noon" (midday, meridies (m.)) is neither before nor after itself, the terms a.m. and p.m. do not apply. Although "12 m." was suggested as a way to indicate noon, this is seldom done and also does not resolve the question of how to indicate midnight."

9
  • 60
    @SamHuckaby: It is even more easy: $timestamp = strtotime('today'); is midnight. But pssst the one above looks cooler as an answer to the question ;)
    – hakre
    Oct 29, 2012 at 21:30
  • I actually need to get start of today and midnight of today. How can I find start of today I mean for 00:00:00 Nov 22, 2013 at 9:23
  • 3
    @AlwinAugustin: You should consider to differ your wording. Midnight is technically between two days. So there is somewhat "no today any longer". Also: Midgnight of today (per definition) is (right before) the start of today. You probably mean the next midnight after today noon.
    – hakre
    Nov 22, 2013 at 9:58
  • 3
    Keep in mind this will produce the midnight of your PHP server's timezone, not UTC+0! To get UTC+0 you can use strtotime('today+00:00').
    – mae
    Apr 18, 2018 at 22:09
  • 1
    @mae nice, there are even alternative ways like today+0, today Z, UTC today, Asia/Shanghai today or HKST today. I have problems to find this in the PHP docs specifically, 3v4l.org link which was missing so far: 3v4l.org/amfGc
    – hakre
    Feb 3, 2020 at 21:51
82

I think that you should use the new PHP DateTime object as it has no issues doing dates beyond the 32 bit restrictions that strtotime() has. Here's an example of how you would get today's date at midnight.

$today = new DateTime();
$today->setTime(0,0);

Or if you're using PHP 5.4 or later you can do it this way:

$today = (new DateTime())->setTime(0,0);

Then you can use the echo $today->format('Y-m-d'); to get the format you want your object output as.

PHP DateTime Object

5
  • 15
    I also prefer using the new DateTime object. If you combine this answer with @hakr's you get: new DateTime('today midnight'), which makes the intention more clear (but this is of course a matter of taste).
    – Martijn
    Nov 22, 2014 at 10:23
  • 1
    I'm doing ORM SQL work and I had to use the DateTime object for comparisons. This is exactly what I was looking for. +1 Jan 9, 2015 at 23:00
  • May be $today = new DateTime('today')
    – ProgZi
    Jul 10, 2016 at 22:02
  • Also useful if you are using Carbon, which inherits from DateTime. Thus, you can also do new Carbon('today midnight') and then you can use all the Carbon stuff like ->subDays(6). See carbon.nesbot.com Aug 28, 2017 at 14:57
  • 1
    Regarding the 32 bit restriction: The PHP docu for strtotime states: "For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction." So it only applies to 32bit php. Aug 28, 2017 at 15:04
22

Today at midnight. Easy.

$stamp = mktime(0, 0, 0);
8

You are looking to calculate the time of the most recent celestial event where the sun has passed directly below your feet, adjusted for local conventions of marking high noon and also potentially adjusting so that people have enough daylight left after returning home from work, and for other political considerations.

Daunting right? Actually this is a common problem but the complete answer is location-dependent:

$zone = new \DateTimeZone('America/New_York'); // Or your own definition of “here”
$todayStart = new \DateTime('today midnight', $zone);
$timestamp = $todayStart->getTimestamp();

Potential definitions of “here” are listed at https://secure.php.net/manual/en/timezones.php

1
  • Like strtotime('America/New_York today midnight');? - 3v4l.org/I85qD
    – hakre
    Mar 13, 2020 at 21:25
6

Updated Answer in 19 April, 2020

Simply we can do this:

$today = date('Y-m-d 00:00:00');

5
$today_at_midnight = strtotime(date("Ymd"));

should give you what you're after.

explanation

What I did was use PHP's date function to get today's date without any references to time, and then pass it to the 'string to time' function which converts a date and time to a epoch timestamp. If it doesn't get a time, it assumes the first second of that day.

References: Date Function: http://php.net/manual/en/function.date.php

String To Time: https://www.php.net/manual/en/function.strtotime.php

3
  • timestamp? That's just a date.
    – wesside
    Oct 29, 2012 at 21:19
  • Is there a particular format that the OP asked for? If so, I would be happy to modify my post to return any format they would like.
    – SamHuckaby
    Oct 29, 2012 at 21:23
  • completely overlooked strtotime(), mind was so focused on the date function. Apologies.
    – wesside
    Oct 29, 2012 at 21:25
2
$timestamp = strtotime('today midnight');

is the same as

$timestamp = strtotime('today');

and it's a little less work on your server.

2

In more object way:

$today = new \DateTimeImmutable('today');

example:

 echo (new \DateTimeImmutable('today'))->format('Y-m-d H:i:s');
// will output: 2019-05-16 00:00:00

and:

echo (new \DateTimeImmutable())->format('Y-m-d H:i:s');
echo (new \DateTimeImmutable('now'))->format('Y-m-d H:i:s');
// will output: 2019-05-16 14:00:35
1

$midnight = strtotime('midnight'); is valid
You can also try out strtotime('12am') or strtotime('[input any time you wish to here. e.g noon, 6pm, 3pm, 8pm, etc]'). I skipped adding today before midnight because the default is today.

0
0

If you are using Carbon you can do the following. You could also format this date to set an Expire HTTP Header.

Carbon::parse('tomorrow midnight')->format(Carbon::RFC7231_FORMAT)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.