Thursday 13 February 2020

Everything about PHP Laravel Carbon time

        // Set default timezone in UTC
        date_default_timezone_set('UTC');
        // Get the current time right now in UTC then convert to pacific time
        // returns carbon time object, if print it will be a string 2020-02-13 16:29:40
        $carbon_time  = Carbon::now('America/Los_Angeles');
        // Get start of year  // current_year-01-01 00:00:00
        $carbon_start_of_year = Carbon::now('America/Los_Angeles')->startOfYear();
        // Find difference in day
        // Difference in day is a string
        $difference_in_day = $carbon_start_of_year->diffInDays($carbon_time);
        // Format year from 4 characters to 2 characters, (from 2020 to 20)
        $year = substr( $carbon_time->year, -2); 


PHP default date time parameters:
https://www.php.net/manual/en/function.date.php


Carbon:
https://carbon.nesbot.com/docs/

No comments:

Post a Comment