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'?
When “Z” (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time. What is T between date and time? The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC).
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.
Timestamps in MySQL are generally used to track changes to records, and are often updated every time the record is changed. If you want to store a specific value you should use a datetime field.
Just as DATETIME , the TIMESTAMP data type contains both the date and the time in the following format YYYY-MM-DD hh:mm:ss . However, unlike DATETIME , the TIMESTAMP data type has a fixed range between 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC.
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();
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With