Search code examples
laravelapiphp-carbonclockify

How to automatically catch the day on Carbon?


enter image description here

I am creating through laravel a system to get clockify all the tasks done on the day between a certain time. I'm looking at carbon for some functionality to automatically catch the day, but I didn't find anything. Does anyone know how to automatically catch the day?


Solution

  • Carbon has the startOfDay() and endOfDay() methods:

    $params->startDate = Carbon::now()->startOfDay()->toJSON();
    $params->endDate = Carbon::now()->endOfDay()->toJSON();
    

    From the Carbon docs:

        /**
         * Resets the time to 00:00:00 start of day
         *
         * @example
         * ```
         * echo Carbon::parse('2018-07-25 12:45:16')->startOfDay();
         * ```
         *
         * @return static
         */
        public function startOfDay();
    
        /**
         * Resets the time to 23:59:59.999999 end of day
         *
         * @example
         * ```
         * echo Carbon::parse('2018-07-25 12:45:16')->endOfDay();
         * ```
         *
         * @return static
         */
        public function endOfDay();
    
        /**
         * Return the ISO-8601 string (ex: 1977-04-22T06:00:00Z) with UTC timezone.
         *
         * @example
         * ```
         * echo Carbon::now('America/Toronto')->toJSON();
         * ```
         *
         * @return null|string
         */
        public function toJSON();