<?php

/**
|--------------------------------------------------------------------------
| Global Date/Time Functions
|--------------------------------------------------------------------------
|
| This is a home for functions that don't belong to any one class and
| that should be available anywhere in the application.
|
*/

if (! function_exists('secondsToTime')) {
    /**
     * Convert seconds to hour:minute:second with hour being omitted
     * if the amount of seconds is less than an hour.
     *
     * @since 1.0.0
     *
     * @param datatype $seconds
     *
     * @return string
     */
    function secondsToTime($seconds): string
    {
        $seconds = intval($seconds);
        if ($seconds >= 3600) {
            return sprintf('%02d:%02d:%02d', ($seconds / 3600), (($seconds / 60) % 60), ($seconds % 60));
        }

        return sprintf('%02d:%02d', (($seconds / 60) % 60), ($seconds % 60));
    }
}

if (! function_exists('minutesToTime')) {
    /**
     * Convert minutes to HH:MM
     *
     * @since 1.0.0
     *
     * @param datatype $minutes
     *
     * @return string
     */
    function minutesToTime($minutes): string
    {
        $minutes = intval($minutes);

        return sprintf('%d:%02d', floor($minutes / 60), ($minutes % 60));
    }
}

if (! function_exists('jddayofweek')) {
    /**
     * Returns the day of the week. Can return a string or an integer depending on the mode.
     *
     * @since 1.0.0
     *
     * @param int|null $intDay
     * @param int $mode
     *
     * @return string
     */
    function jddayofweek(?int $intDay = null, int $mode = 0): string
    {
        if (is_null($intDay)) {
            $intDay = date('l');
        }

        if ($mode === 0) {
            return $intDay;
        }

        return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][$intDay];
    }
}
