Your IP : 216.73.216.79


Current Path : /var/www/html/school/common/components/
Upload File :
Current File : /var/www/html/school/common/components/Helpers.php

<?php

namespace common\components;

class Helpers {

    /**
     * to return num of working days between two date
     * date is in format Y-m-d. by default off day is saturday and sunday
     * 1 = monday, 7 = sunday
     */
    public static function workingDays($dt1, $dt2, $off_day = array(6, 7)) {
        $working_days = 0;
        
        $num_days = MyDate::dateDiff($dt1, $dt2, 'd') + 1;
        $date1 = new \DateTime($dt1);
        
        for ($i = 1; $i <= $num_days; $i++) {
            $day = $date1->format('N');
            $is_off = false;
            foreach ($off_day as $off) {
                if ($day == $off) {
                    $is_off = true;
                }
            }

            if (!$is_off) {
                $working_days++;
            }

            $date1->add(new \DateInterval('P1D'));
        }
        return $working_days;
    }

    /**
     * to return string date from a format to other given format
     * @param <type> $from = dmy / mdy /ymd
     * @param <type> $to = dmy / mdy /ymd
     * @param <type> $val
     * @param <string> $marker either - or /
     * @return <type>
     */
    public static function dateFormat($from = 'dmy', $to = 'ymd', $val = '', $marker = '-') {
        if ($from == '' || $to == '' || $val == '' || $val == '0000-00-00')
            return '';

        if ($from == 'dmy') {
            $day = substr($val, 0, 2);
            $month = substr($val, 3, 2);
            $year = substr($val, 6, 4);
        } else if ($from == 'mdy') {
            $month = substr($val, 0, 2);
            $day = substr($val, 3, 2);
            $year = substr($val, 6, 4);
        } else if ($from == 'ymd') {
            $year = substr($val, 0, 4);
            $month = substr($val, 5, 2);
            $day = substr($val, 8, 2);
        }

        if ($to == 'dmy')
            return $day . $marker . $month . $marker . $year;
        else if ($to == 'ymd')
            return $year . $marker . $month . $marker . $day;
        else if ($to == 'mdy')
            return $month . $marker . $day . $marker . $year;
    }

    public static function monthList() {
        $month = array(
            '0' => '--Pilih Bulan--',
            '01' => 'Jan',
            '02' => 'Feb',
            '03' => 'Mar',
            '04' => 'Apr',
            '05' => 'Mei',
            '06' => 'Jun',
            '07' => 'Jul',
            '08' => 'Aug',
            '09' => 'Sep',
            '10' => 'Oct',
            '11' => 'Nov',
            '12' => 'Dec',
        );
        return $month;
    }

    public static function monthRange($from, $to) {
        $month = array('0' => '--Sila Pilih--');
        for ($i = $from; $i <= $to; $i++) {
            $month[$i] = $i;
        }
        return $month;
    }

    public static function yearList($yr_show, $from_offset = 0) {
        $year = array('0' => '--Pilih Tahun--');
        $yr_from = date('Y') + $from_offset;
        $yr_to = $yr_from - $yr_show;
        for ($i = $yr_to; $i <= $yr_from; $i++) {
            $year[$i] = $i;
        }
        return $year;
    }

    /**
     * return perbezaan masa antara dua tarikh samada second, minit, hari, minggu, bulan, tahun
     * @param string $dt_from tarikh dari i.e 01/01/2011
     * @param string $dt_to tarikh hingga 30/01/2011
     * @param char $time_part s=second, m=minit, h=hour, d=day, w=week, mt=month, y=year
     * @return int 
     */
    public static function dateDiff($dt_from, $dt_to, $time_part) {
        $dt_from2 = strtotime($dt_from); // date dlm second
        $dt_to2 = strtotime($dt_to); // date dlm second
        $sec = $dt_to2 - $dt_from2;

        switch ($time_part) {
            case 's' :
                return $sec;
                break;
            case 'm' :
                $min = $sec / 60;
                return $min;
                break;
            case 'h' :
                $hour = $sec / (60 * 60);
                return $hour;
                break;
            case 'd' :
                $day = $sec / (60 * 60 * 24);
                return $day;
                break;
            case 'y' :
                $year = $sec / (60 * 60 * 24 * 365.25);
                return (int) $year;
                break;
            case 'mt' :
                $month = $sec / (60 * 60 * 24 * 30);
                return ceil($month); // anggaran terhampir
                break;
            default :
                return 0;
        }
    }

    public static function truncate($string, $width)
    {
        $parts = preg_split('/([\s\n\r]+)/u', $string, null, PREG_SPLIT_DELIM_CAPTURE);
        $parts_count = count($parts);

        $length = 0;
        $last_part = 0;
        for (; $last_part < $parts_count; ++$last_part) {
            $length += strlen($parts[$last_part]);
            if ($length > $width) { break; }
        }

        return implode(array_slice($parts, 0, $last_part))."...";
    }

    public static function jsonerrcode(int $err){
        switch ($err) {
        case JSON_ERROR_NONE:
            return 'No errors';
        break;
        case JSON_ERROR_DEPTH:
            return 'Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            return 'Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            return 'Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            return 'Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            return 'Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            return 'Unknown error';
        break;
    }

    }

}