List

Try: ca/tecreations/ViewFile.php -- Online


<?php


class Time {
    var $now;
    var $nowDT;
    var $nowYMD;
    var $isLeap;
    var $month;
    var $day;
    var $sol;
    var $nowHMS;
    var $nowMicro;
    var $nowTime;
    var $nowPlus10;
    var $nowPlus30;
    var $nowPlus60;

    var $isLeapYear;

    var $weekOfYear;
    var $woy;
    var $dayOfWeek;
    var $dow;
    var $time;
    var $ampm;

    public function __construct() {
        $this->now = new Datetime('now'); //new DateTimeZone("UTC")); 
        $this->nowHMS = $this->now->format('Y-m-d H:i:s');
        $this->nowMicro = $this->now->format('Y-m-d H:i:s.u');
        $this->nowTime = $this->now->format('H:i:s');
        $this->nowYMD = $this->now->format('Y-m-d');
        $this->nowPlus10M = mktime(date("h"),date("i")+10,date("s"), date("m") , date("d"), date("Y"));
        $this->nowPlus30M = mktime(date("h"),date("i")+30,date("s"), date("m") , date("d"), date("Y"));
        $this->nowPlus60M = mktime(date("h")+1,date("i"),date("s"), date("m") , date("d"), date("Y"));
        global $DEBUG, $VERBOSE;
        if ($DEBUG && $VERBOSE) {
            print("Please address the debug code in /ca/tecreations/Time.php");
//            print("Now: All: $" . "nowAll: $nowAll<br />\n");
//            print("Now: HMS: $" . "nowHMS: $nowHMS<br />\n");
//            print("Now: micro: $" . "nowMicro: $nowMicro<br />\n");
//            print("Now: Time: $" . "nowTime: $nowTime<br />\n");
//            print("Now: UNIX: strtotime(\$" . "nowMicro): " . strtotime($nowMicro) . "<br />\n");
//            print("Now: YMD: $" . "nowYMD: $nowYMD<br />\n");
//            print("<hr />");
//            print_r($now); print("<br />");
//            print("NowYMD: $nowYMD<br />");
//            print("NowWithHMS: $nowYMD" . "T" . "$nowTime<br />");
//            print("DT: " . $nowMicro);
        }
        
        if ($this->isLeapYear((int)$this->now->format("Y"))) {
            $this->isLeap = true;
        } else {
             $this->isLeap = false;
        }

        $this->month = (int)$this->now->format("m");
        $this->day = (int)$this->now->format("d");
	$this->sol = $this->getSol($this->month, $this->day);
        $this->weekOfYear = ((int)$this->sol / 7);
        $this->woy = $this->weekOfYear;
        $this->dayOfWeek = $this->now->format("w");
        $this->time= $this->now->format("H:i");
        if ($this->time[0] == '0') {
            $this->time = substr($this->time,1);
        }
        $this->ampm = ((int)$this->now->format("H") > 11 ? "PM" : "AM");
    }

    function getDayOfWeek($date) {
        $dayIndex = $date->format("w");
        if ($dayIndex == 0) return "Sunday";
        if ($dayIndex == 1) return "Monday";
        if ($dayIndex == 2) return "Tuesday";
        if ($dayIndex == 3) return "Wednesday";
        if ($dayIndex == 4) return "Thursday";
        if ($dayIndex == 5) return "Friday";
        if ($dayIndex == 6) return "Saturday";
    }       
    
    function getMarchOn($month = 3, $day = 1) {
        $march = 31;
        $april = 30;
        $may = 31;
        $june = 30;
        $july = 31;
        $august = 31;
        $september = 30;
        $october = 31;
        $november = 30; 
        if ($month == 3) {
            return $day;
        } else if ($month == 4) {
  // totally! It begs the how many days in the month
            return (int)$march + (int)$day;
        } else if ($month == 5) {
            return (int)$march + (int)$april + (int)$day;
        } else if ($month == 6) {
            return (int)$march + (int)$april + (int)$may + (int)$day;
        } else if ($month == 7) {
            return (int)$march + (int)$april + (int)$may + (int)$june + (int)$day;
        } else if ($month == 8) {
            return (int)$march + (int)$april + (int)$may + (int)$june + (int)$july + (int)$day;
        } else if ($month == 9) {
            return (int)$march + (int)$april + (int)$may + (int)$june + (int)$july + (int)$august + (int)$day;
        } else if ($month == 10) {
            return (int)$march + (int)$april + (int)$may + (int)$june + (int)$july + (int)$august + 
            (int)$september + (int)$day;
        } else if ($month == 11) {
            return (int)$march + (int)$april + (int)$may + (int)$june + (int)$july + (int)$august + 
            (int)$september + (int)$october + (int)$day;
        } else if ($month == 12) {
            return (int)$march + (int)$april + (int)$may + (int)$june + (int)$july + (int)$august + 
            (int)$september + (int)$october + (int)$november + (int)$day;
        }
    }    


    function getDaysBetween_DateTime($dt1 = "", $dt2 = "") {
        return (($dt1)->diff($dt2))->days;
    }

    function getDaysBetween_Timestamp($timestamp1 = "", $timestamp2 = "") {
        return (new DateTime($timestamp1))->diff(new DateTime($timestamp2))->days;
    }

    function getDaysInMonth_DT($DateTime = "") { 
        if ($DateTime == "") die("getDaysInMonth_DT() -- DateTime is empty");
        $month = $DateTime->format("m");
        return getDaysInMonth_String($month);
    }

    function getDaysInMonth_int($month = "") {
        if ($month == "") die("getDaysInMonth_int() -- month is empty");
        if ($month == "02") {
           if (isLeapYear($setAt->format("Y"))) $daysInMonth = 29;
           else $daysInMonth = 28;
        } else if ($month == "04") $daysInMonth = 30;
        else if ($month == "06") $daysInMonth = 30;
        else if ($month == "09") $daysInMonth = 30;
        else if ($month == "11") $daysInMonth = 30;
        else $daysInMonth = 31;
        return (int)$daysInMonth;
    }

    function getDaysUntil_DateTime($dt1 = "", $dt2 = "") {
        return (($dt1)->diff($dt2))->days + 1;
    }

    function getFormattedDateTime($ts = "") {
        return date("Y-m-d H:i:s",$ts);
    }
 
    function getFormattedHMS($ts = "") {
        return date("H:i:s",$ts);
    }

    function getFormattedTZOffset() {
        $zoneOffset = "";
        $fifteenMinutes = 15 * 60;
        $zone = $this->now->format('Z');
        $isMinus = ($zone[0] == "-");
        $quarters = (int)$this->now->format('Z') / $fifteenMinutes * -1;
        $hours = 0;
        while ($quarters >= 4) {
            $quarters -= 4;
            $hours++;
        }
        if ($hours < 10) {
            $zoneOffset = "0" . $hours;
        } else {
            $zoneOffset = $hours;
        }
        if ($quarters == "0") $zoneOffset = $zoneOffset . "00";
        if ($quarters == "1") $zoneOffset = $zoneOffset . "15";
        if ($quarters == "2") $zoneOffset = $zoneOffset . "30";
        if ($quarters == "3") $zoneOffset = $zoneOffset . "45";
        if ($isMinus) $zoneOffset = "-" . $zoneOffset;
        return $zoneOffset;
    }

    function getFormattedYMD($ts = "") {
        return date("Y-m-d",$ts);
    }

    function getMicro($ts = "") {
        return date("Y-m-d H:i:s.u",$ts);
    }

    function getNow() { return $this->now; }

    function getNowDateTime() { return $this->nowDT; }

    function getNowYMD() { return $this->nowYMD; }

    function getNowHMS() { return $this->nowHMS; }

    function getNowMicro() { return $this->nowMicro; }

    function getNowPlus10() { 
        $then = mktime(date("h"),date("i") + 10, date("s"), date("m") , date("d"), date("Y")); 
        return $then;
    } 

    function getNowPlus30() { 
        $then = mktime(date("h"),date("i") + 30, date("s"), date("m") , date("d"), date("Y")); 
        return $then;
    }

    function getNowPlus60() { 
        $then = mktime(date("h"),date("i") + 60, date("s"), date("m") , date("d"), date("Y")); 
        return $then;
    }


    function getNowTime() { return $this->nowTime; }

    function getSecondsBetween_Timestamp($timestamp1 = "", $timestamp2 = "") {
        return (strtotime($timestamp2) - strtotime($timestamp1));
    }

    function getShortMonth() {
        $month = date("F");
        if ($month == "January") return "Jan";
        else if ($month == "February") return "Feb";
        else if ($month == "March") return "Mar";
        else if ($month == "April") return "Apr";
        else if ($month == "May") return "May";
        else if ($month == "June") return "Jun";
        else if ($month == "July") return "Jul";
        else if ($month == "August") return "Aug";
        else if ($month == "September") return "Sep";
        else if ($month == "October") return "Oct";
        else if ($month == "November") return "Nov";
        else if ($month == "December") return "Dec";
    }

    function getSol($month = 1, $day = 1) {
        $sol = 0;
        if ($month == 1) {
            $sol = $day;
        } else if ($month == 2) {
            $sol = 31 + $day;
        } else {
            $sol = 31 + 28;
            if ($this->isLeapYear()) $sol++;
            for($i = 3; $i <= $month - 1;$i++) {
               $sol += $this->getDaysInMonth_int($i);
            }
            $sol += $day;
        }
        return $sol;
    }

    function getTimeAsDigits($str = "") {
        return strtotime(str);    
    }

    function getTimeZoneOffset($dateTimeObject) {
        return $dateTimeObject->format('Z') / 3600;
    }

    function getTimsTime() {
        return "D" . $this->now->format('Y-m-d') . "T" . $this->now->format('H:i:s.u') . "Z" . $this->getFormattedTZOffset($this->now);
    }

    //https://www.geeksforgeeks.org/php-program-to-check-a-given-year-is-a-leap-year-or-not/
    function isLeapYear($year = 1970) {
        if (($year % 4 == 0 && $year % 100 != 0)  
            || ($year % 400 == 0)) { 
            return true; 
        } else { 
            return false; 
        } 
    }
}
global $time;
$time = new Time();