Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store timestamp with time 00:00:00

Tags:

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

like image 744
html_programmer Avatar asked Dec 29 '14 17:12

html_programmer


People also ask

What is the Z at the end of a TIMESTAMP?

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).

What is the format of TIMESTAMP?

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.

Is it better to use TIMESTAMP or datetime?

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.

Is TIMESTAMP same as datetime?

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.


Video Answer


2 Answers

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(); 
like image 185
Abdalla Arbab Avatar answered Oct 09 '22 17:10

Abdalla Arbab


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); } 
like image 35
Alister Bulman Avatar answered Oct 09 '22 17:10

Alister Bulman