Search code examples
phpdatetimeformat

Is this the proper way to get the end of day in PHP?


I'm getting the End Of Day like this:

function endofday($date_to_change)
{
  $date_to_change = date('Y-m-d');
  $date_to_change = date('Y-m-d H:i:s', strtotime("+23 hours 59 minutes  59 seconds", strtotime($date_to_change)));
  return date("Y-m-d H:i:s", $date_to_change);
}

It works but is this the proper and most efficient way to get 23:59:59 from a date? My goal is to select rows from a mysql database between 00:00:00 and 23:59:59 from a date passed in with or without time.


Solution

  • Можно и так
    $begin = date('Y-m-d 00:00:00');
    $end = date('Y-m-d 23:59:59',strtotime($date_to_change));