Given the Unix timestamp, how to start and end this day?

I have a Unix timestamp:

$timestamp=1330581600 

How do I get the beginning of the day and end of the day for this timestamp?

 eg $beginOfDay = Start of Timestamp Day $endOfDay = End of Timestamp Day 

I tried this:

 $endOfDay = $timestamp + (60 * 60 * 23); 

But I don’t think this will work, because the timestamp itself is not the exact beginning of the day.

+70
php timestamp unix-timestamp strtotime
Apr 17 '12 at 19:33
source share
7 answers

strtotime can be used to quickly beat hours / minutes / seconds

 $beginOfDay = strtotime("midnight", $timestamp); $endOfDay = strtotime("tomorrow", $beginOfDay) - 1; 

DateTime can also be used, although several additional steps are required to get 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; // Go to midnight. ->modify('midnight') does not do this for some reason $beginOfDay->modify('today'); $endOfDay = clone $beginOfDay; $endOfDay->modify('tomorrow'); // adjust from the next day to the end of the day, per original question $endOfDay->modify('1 second ago'); var_dump( array( 'time ' => $dtNow->format('Ymd H:i:s e'), 'start' => $beginOfDay->format('Ymd H:i:s e'), 'end ' => $endOfDay->format('Ymd H:i:s e'), ) ); 
+162
Apr 17 '12 at 19:39
source share

Just datetime

 $beginOfDay = DateTime::createFromFormat('Ymd H:i:s', (new DateTime())->setTimestamp($timestamp)->format('Ymd 00:00:00'))->getTimestamp(); $endOfDay = DateTime::createFromFormat('Ymd H:i:s', (new DateTime())->setTimestamp($timestamp)->format('Ymd 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 that sets the hour / minute / second at the beginning or end of the day. Finally, a new DateTime is created from this line and the timestamp is retrieved.

Read

 $dateTimeObject = new DateTime(); $dateTimeObject->setTimestamp($timestamp); $beginOfDayString = $dateTimeObject->format('Ymd 00:00:00'); $beginOfDayObject = DateTime::createFromFormat('Ymd H:i:s', $beginOfDayString); $beginOfDay = $beginOfDayObject->getTimestamp(); 

We can get the end of the day in an alternative way 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(); 

Timezone

You can also set the time zone by adding a time indicator to a format such as O and specifying a timestamp after creating the DateTime object:

 $beginOfDay = DateTime::createFromFormat('Ymd H:i:s O', (new DateTime())->setTimezone(new DateTimeZone('America/Los_Angeles'))->setTimestamp($timestamp)->format('Ymd 00:00:00 O'))->getTimestamp(); 

Flexibility DateTime

We can also get other information, such as the beginning / end of the month or the beginning / end of the hour by changing the specified second format. Monthly: 'Ym-01 00:00:00' and 'Ymt 23:59:59' . Per hour: 'Ymd H:00:00' and 'Ymd 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 you will need to take care to cope with leap years correctly.

Relevant Links

From the PHP docs:

+10
Jan 04 '16 at 18:07
source share

You can use a combination of date() and mktime() :

 list($y,$m,$d) = explode('-', date('Ym-d', $ts)); $start = mktime(0,0,0,$m,$d,$y); $end = mktime(0,0,0,$m,$d+1,$y); 

mktime() is smart enough to carry months / years when a day is given outside the specified month (jan 32nd will be feb 1st, etc.)

+5
Apr 17 '12 at 19:38
source share

You can convert the time to current data, and then use the strtotime function to find the beginning of the day, and simply add 24 hours to this to find the end of the day.

You can also use the remainder operator (%) to find the next day. For example:

 $start_of_day = time() - 86400 + (time() % 86400); $end_of_day = $start_of_day + 86400; 
+4
Apr 17 '12 at 19:36
source share

Today is the start date of the timestamp. Plain

 $stamp = mktime(0, 0, 0); echo date('mdY H:i:s',$stamp); 
+2
May 19 '16 at 6:56 a.m.
source share

For those who have this question in the future:

Code of any day

 <?php $date = "2015-04-12 09:20:00"; $midnight = strtotime("midnight", strtotime($date)); $now = strtotime($date); $diff = $now - $midnight; echo $diff; ?> 

Current day code

 <?php $midnight = strtotime("midnight"); $now = date('U'); $diff = $now - $midnight; echo $diff; ?> 
0
Mar 30 '15 at 9:29
source share
 $start_of_day = floor (time() / 86400) * 86400; $end_of_day = ceil (time() / 86400) * 86400; 

If you need both values ​​in the same script. For one of the variables, +/- 86,400 seconds is faster than shooting both in half and on the ceiling. For example:

 $start_of_day = floor (time() / 86400) * 86400; $end_of_day = $start_of_day + 86400; 
0
Mar 22 '16 at 22:56
source share



All Articles