0

I have a timestamp say 1512070200 which translates to November 30, 2017 7:30:00 PM GMT or December 1, 2017 01:00:00 AM GMT+05:30 in IST.

I want to be able to subtract the time that has passed on that day and revert back to the time at 12:00:00 AM.

For example

If i get a timestamp of 1512978955 which is December 11, 2017 7:55:55 AM GMT I want the output to be 1512950400 which is December 11, 2017 12:00:00 AM GMT.

There is no fixed amount of hours that can be subtracted from the timestamp, instead it would be a variable amount depending on the time that has passed for that particular day so it could be 1 millisecond or 1 second or 1 minute or 1 hour since 12:00:00 AM.

Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • Possible duplicate of [unix timestamp round to midnight](https://stackoverflow.com/questions/8550236/unix-timestamp-round-to-midnight) – Nigel Ren Dec 11 '17 at 08:12

2 Answers2

2

One way would be to convert the timestamp to a date and then break into it's constituent parts so that you can use mktime to generate the date at midday

$ts=1512070200;

$y=date('Y',$ts);
$m=date('m',$ts);
$d=date('d',$ts);

$date=date('Y-m-d H:i:s',mktime(12,0,0,$m,$d,$y));
echo date('Y-m-d H:i:s',$ts).' -> '.$date;

which outputs

2017-11-30 19:30:00 -> 2017-11-30 12:00:00

or, if the output date needs to be as shown use

$format=DATE_COOKIE;
$ts=1512070200;

$y=date('Y',$ts);
$m=date('m',$ts);
$d=date('d',$ts);

$date=date($format,mktime(12,0,0,$m,$d,$y));
echo date($format,$ts).' -> '.$date;

#Thursday, 30-Nov-17 19:30:00 GMT -> Thursday, 30-Nov-17 12:00:00 GMT

Even easier is to use noon as shown by @splash58

echo date( $format, strtotime( 'noon', $ts ) );
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
1

You can set noon time to DateTime object

$ts=1512070200;

$date = new DateTime();
$date->setTimestamp($ts);
$date->modify('noon');
echo $date->format(DATE_COOKIE); // Thu, 30 Nov 2017 12:00:00 +0000

demo

splash58
  • 26,043
  • 3
  • 22
  • 34