-2

I have a variable called $end_date which gives me the date in epoch format like this: 1539129600. This translates to Wednesday 10 October 2018 00:00:00.

The variable returns different dates, but the time is always the same 00:00:00. Is it possible to take this variable, set the time to 23:59:59 and save it back to a new variable called $end_date_time containing the same date and the new time 23:59:59 in epoch format.

joq3
  • 145
  • 7

1 Answers1

1

You can do this with the DateTime class, using setTimestamp to convert the timestamp and then setTime to set the time of day, finally getting the new timestamp with format:

$end_date = 1539129600;
$end_of_day = new DateTime(null, new DateTimeZone('UTC'));
$end_of_day->setTimestamp($end_date);
echo $end_of_day->format('Y-m-d H:i:s') . "\n";
$end_of_day->setTime(23,59,59);
echo $end_of_day->format('Y-m-d H:i:s') . "\n";
echo (int)$end_of_day->format('U');

Output:

2018-10-10 00:00:00 
2018-10-10 23:59:59 
1539215999

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95