Save timestamp with time 00:00:00

I want to store a timestamp in a database with a 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 the date only without a time specification.
I need this because after that I need to get the inner integer from the database, which should be the start of the day.

What is the best way to keep this "start of the day"?

+3


source to share


3 answers


I usually set the date with a call and then reset the time to 00:00 with

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

      



The class also has a Carbon :: startOfDay () method that sets the appropriate values.

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

      

+6


source


For safety, just let Carbon decide the end or start of the day with ->endOfDay()

or ->startOfDay()

. Choose what suits you best.



Carbon::createFromFormat('d-m-Y', $date_interval["start_date"])->endOfDay();
Carbon::createFromFormat('d-m-Y', $date_interval["start_date"])->startOfDay();

      

+5


source


You shouldn't be doing this.

set start_date

DATE NULL DEFAULT NULL`

You have to allow MySQL default NULL not 0000-00-00

0


source







All Articles