Select a midnight timestamp before a given timestamp

Given any unix timestamp, I want to get the midnight timestamp to T.

This timestamp can be any integer: now, today (not too far []) in the future or (not too far []) in the past.

Is there a cleaner path (pseudocode):

<?php $midnight = strtotime("{date('d',$ts)}-{date('m',$ts)}-{date('Y', $ts)} midnight"); ?> 

Thanks.

[*] somewhere between 1990 and 2020.

+4
source share
3 answers

I would do it like

 $midnight = strtotime(date('Ym-d',$ts).' 00:00:00'); 

... but is it cleaner / better controversial ...

+6
source

Since the full day is 86400 seconds, you should do this:

 $midnight = $ts - $ts%86400; 

However, this would not account for any leap seconds that could be applied to the unix timestamp.

It will be much more realistic than using strtotime, but it will probably only matter if you do it in large hard loops.

+3
source

Et voila '

 <?php $midnight = strtotime(date('dm-Y',$ts)); echo date('dmY H:i:s',$ts); // output 03-11-2011 12:43:41 echo date('dmY H:i:s',$midnight); // ouput 03-11-2011 00:00:00 ?> 
+1
source

Source: https://habr.com/ru/post/1379459/


All Articles