Search code examples
phpmysqldatetimephp-carbon

Store timestamp with time 00:00:00


I want to store a timestamp in the database with time component as 00:00:00.
The following code:

$start_date = Carbon::createFromFormat('d-m-Y', $date_interval["start_date"]); 
$end_date = Carbon::createFromFormat('d-m-Y', $date_interval["end_date"]); 

adds the current time to the date, when I provide a date only without time specification.
I need this because I need to retrieve the internal integer from the database afterwards which should be the start of the day.

What is the best way to store this 'start of day'?


Solution

  • I've usually set the date with the call, and then reset the time to 00:00 with

    Carbon::setTime(0, 0, 0);  // from the parent, DateTime class
    

    In the class, there is also a Carbon::startOfDay() method that will set the appropriate values.

    public function startOfDay()
    {
        return $this->hour(0)->minute(0)->second(0);
    }