I have a Unix timestamp like this:
$timestamp=1330581600
How do I get the beginning of the day and the end of the day for that timestamp?
e.g. $beginOfDay = Start of Timestamp's Day $endOfDay = End of Timestamp's Day
I tried this:
$endOfDay = $timestamp + (60 * 60 * 23);
But I don't think it'll work because the timestamp itself isn't the exact beginning of the day.
UNIX_TIMESTAMP() : This function in MySQL helps to return a Unix timestamp. We can define a Unix timestamp as the number of seconds that have passed since '1970-01-01 00:00:00'UTC. Even if you pass the current date/time or another specified date/time, the function will return a Unix timestamp based on that.
1 Hour: 3,600 seconds. 1 Day: 86,400 seconds.
strtotime can be used to to quickly chop off the hour/minutes/seconds
$beginOfDay = strtotime("today", $timestamp); $endOfDay = strtotime("tomorrow", $beginOfDay) - 1;
DateTime can also be used, though requires a few extra steps to get from a long timestamp
$dtNow = new DateTime(); // Set a non-default timezone if needed $dtNow->setTimezone(new DateTimeZone('Pacific/Chatham')); $dtNow->setTimestamp($timestamp); $beginOfDay = clone $dtNow; $beginOfDay->modify('today'); $endOfDay = clone $beginOfDay; $endOfDay->modify('tomorrow'); // adjust from the start of next day to the end of the day, // per original question // Decremented the second as a long timestamp rather than the // DateTime object, due to oddities around modifying // into skipped hours of day-lights-saving. $endOfDateTimestamp = $endOfDay->getTimestamp(); $endOfDay->setTimestamp($endOfDateTimestamp - 1); var_dump( array( 'time ' => $dtNow->format('Y-m-d H:i:s e'), 'start' => $beginOfDay->format('Y-m-d H:i:s e'), 'end ' => $endOfDay->format('Y-m-d H:i:s e'), ) );
With the addition of extended time in PHP7, there is potential to miss a second if using $now <= $end
checking with this. Using $now < $nextStart
checking would avoid that gap, in addition to the oddities around subtracting seconds and daylight savings in PHP's time handling.
$beginOfDay = DateTime::createFromFormat('Y-m-d H:i:s', (new DateTime())->setTimestamp($timestamp)->format('Y-m-d 00:00:00'))->getTimestamp(); $endOfDay = DateTime::createFromFormat('Y-m-d H:i:s', (new DateTime())->setTimestamp($timestamp)->format('Y-m-d 23:59:59'))->getTimestamp();
First a DateTime object is created and the timestamp is set to the desired timestamp. Then the object is formatted as a string setting the hour/minute/second to the beginning or end of the day. Lastly, a new DateTime object is created from this string and the timestamp is retrieved.
$dateTimeObject = new DateTime(); $dateTimeObject->setTimestamp($timestamp); $beginOfDayString = $dateTimeObject->format('Y-m-d 00:00:00'); $beginOfDayObject = DateTime::createFromFormat('Y-m-d H:i:s', $beginOfDayString); $beginOfDay = $beginOfDayObject->getTimestamp();
We can get the end of the day in an alternate manner using this longer version:
$endOfDayObject = clone $beginOfDayOject(); // Cloning because add() and sub() modify the object $endOfDayObject->add(new DateInterval('P1D'))->sub(new DateInterval('PT1S')); $endOfDay = $endOfDayOject->getTimestamp();
The timezone can be set as well by adding a timestamp indicator to the format such as O
and specifying the timestamp after creating the DateTime object:
$beginOfDay = DateTime::createFromFormat('Y-m-d H:i:s O', (new DateTime())->setTimezone(new DateTimeZone('America/Los_Angeles'))->setTimestamp($timestamp)->format('Y-m-d 00:00:00 O'))->getTimestamp();
We can also get other information such as the beginning/end of the month or the beginning/end of the hour by changing the second format specified. For month: 'Y-m-01 00:00:00'
and 'Y-m-t 23:59:59'
. For hour: 'Y-m-d H:00:00'
and 'Y-m-d H:59:59'
Using various formats in combination with add()/sub() and DateInterval objects, we can get the beginning or end of any period, although some care will need to be taken to handle leap years correctly.
From the PHP docs:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With