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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
or via a DateTime:
and then perhaps immutable:
midnight" or or just "today" return the same.UTC today", to have it, always.midnightZ"midnightis since PHP 5.1.2 (Jan 2006),todaysince PHP 4.3.1 (Feb 2003)More examples:
given the time UTC 2020-01-01 00:00:00:
when calling
strtotime($), results are:Online Demo: https://3v4l.org/KWFJl
PHP Documentation:
On the Relative Formats page, see the Day-based Notations table. These formats are for
strtotime(),DateTimeanddate_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")orstrtotime("midnight today")is a complete and well sounding answer for PHP, it may appear a bit verbose asstrtotime("midnight")andstrtotime("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:
Compare with this programming question:
(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?):
or for end of day:
(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:
(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:
and from the Wikipedia 12-hour clock: