28

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'?

14
  • So you need a true 0 or an empty datetime like 0000-00-00 00:00:00? Commented Dec 29, 2014 at 17:35
  • I need a format where I have yyyy-mm-dd 00:00:00. Commented Dec 29, 2014 at 17:37
  • 3
    Why not just use a date field rather than a datetime/timestamp field? This field type does not have time component. Commented Dec 29, 2014 at 17:40
  • 1
    Have you tried: Carbon::createFromFormat('d-m-Y 00:00:00', ..); ;p Commented Dec 29, 2014 at 17:43
  • 2
    There's also the startOfDay() method that you can use to reset the date objects time to 00:00:00, once again in docs Ctrl-F with help you find it. Commented Dec 29, 2014 at 17:50

4 Answers 4

38

For safety just let Carbon decide the end or the start of the day using ->endOfDay() or ->startOfDay(). Choose what suits you more.

Carbon::createFromFormat('d-m-Y', $date_interval["start_date"])->endOfDay();
Carbon::createFromFormat('d-m-Y', $date_interval["start_date"])->startOfDay();
Sign up to request clarification or add additional context in comments.

1 Comment

@Trace is there any way my answer will be chosen, as it's more updated?
34

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);
}

2 Comments

As of v1.24.0, the Carbon library has also added createMidnightDate($year = null, $month = null, $day = null, $tz = null).
In case we have a time field, we can set that directly calling setTimeFromTimeString from the Carbon instance.
2

Carbon::createFromFormat() differs from its parent, in that missing parts are set to the current date or time instead of zero. In order to make it behave like \DateTime::createFromFormat(), use the | or ! format characters, as described in the manual (https://www.php.net/manual/en/datetime.createfromformat.php).

Example:

$midnight = Carbon::createFromFormat(
    'Y-m-d|', // or: '!Y-m-d'
    $date
);

1 Comment

This is really useful, I was never aware of this!
0

You shouldn't do that.

set your start_date DATE NULL DEFAULT NULL`

You should let MySQL default to NULL not 0000-00-00

2 Comments

I think that there is a misunderstanding... I'm not looking for a null value, rather a timestamp where the time is set to the start of the day 00-00-00.
then you should use $start_date = strtotime("midnight");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.