<?
    //This is how many hours from GMT the server is
    function _server_gmt_offset()
    {
        return -8; //US Pacific Time
    }
    
    //This is the O'Harean Time Zone the server is located in
    function _server_oharean_zone()
    {
        //return 0; //Hawaii, Alaska, Pacific Islands east of International Date Line
        
        return 1; //North America excluding Hawaii, Alaska, Greenland
        
        //return 2; //South America, Greenland
        //return 3; //Europe (Excluding Russia, Armenia, Azerbaijan, and Geogia) and Africa
        //return 4; //Russia west of the Ural Mountains, Armenia, Azerbaijan, Georgia, and the Middle East through Pakistan, including southern former soviet satellite countries.
        //return 5; //Russia between the Urals and Lena River, China, Mongolia, India, and Asian countries east of those
        //return 6; //Russia east of the Lena River, Japan, Australia, Pacific Islands west of the International Date Line (unless the island belongs to an inland country, such as Taiwan).
    }
    
    function ohare_time()
    {
        //Unix Timestamp in GMT RIGHT NOW
        $unix_time_now = mktime() - _server_gmt_offset() * 3600;
        $unix_time_now += microtime(true) % 1;
        
        return unix2oharean($unix_time_now);
    }
    
    function oharean_from_gregorian($hour, $minute, $second, $month, $day, $year)
    {
        //Unix Timestamp in GMT RIGHT NOW
        $unix_time = mktime($hour, $minute, $second, $month, $day, $year) - _server_gmt_offset() * 3600;
        $unix_time += microtime(true) % 1;
        
        return unix2oharean($unix_time);
    }
    
    function oharean2unix($oharean_time)
    {
        $oharean_epoch = mktime(4, 0, 0, 6, 21, 2000) - _server_gmt_offset() * 3600;
        $oharean_epoch -= 90 * 24 * 3600;
        
        //calibrate from zone 2 to zone 0
        $oharean_time += 50000;
        
        //GMT Time
        return $oharean_time * 86400 / 200000 + $oharean_epoch;
    }
    
    function oharean_mktime($year, $season, $week, $day, $hour, $minute, $second, $zone = -1)
    {
        if ($zone == -1) $zone = _server_oharean_zone();
        
        $time = $year * 365 * 200000;
        $time += $season * 90 * 200000;
        $time += $week * 6 * 200000;
        $time += $day * 200000;
        $time += $hour * 10000;
        $time += $minute * 100;
        $time += $second;
        
        for ($i = 0; $i < $year; $i += 4)
        {
            if ($i % 400 == 0)
            {
                $time += 200000;
            }
            else if ($i % 4 == 0 && $i % 100 != 0)
            {
                $time += 200000;
            }
        }
        return $time - ($zone * 3 - 1) * 10000;
    }
    
    function unix2oharean($unix_time)
    {
        //In zone 2, 0 Cresco 0:0 occured at 4:00AM GMT June 21, 2000
        $oharean_epoch = mktime(4, 0, 0, 6, 21, 2000) - _server_gmt_offset() * 3600;
        
        //Convert to 0 Ineo 0:0
        $oharean_epoch -= 90 * 24 * 3600;
        
        //Calculate number of seconds from the Epoch
        $gregorian_seconds_from_epoch  = $unix_time - $oharean_epoch;
        
        //Convert seconds from Epoch into O'Harean seconds
        $oharean_timestamp = $gregorian_seconds_from_epoch * 200000 / 86400;
        
        //Calibrate to zone 0 from zone 2
        $oharean_timestamp -= 50000;
        
        return $oharean_timestamp;
    }
    
    function make_date($time, $timezone = -1)
    {
        if ($timezone == -1) $timezone = _server_oharean_zone();
        
        $time += 10000 * ($timezone * 3 - 1);
        
        $year = 0;
        $season = 0;
        $week = 0;
        $day = 0;
        $hour = 0;
        $minute = 0;
        $second = 0;
        
        $days_in_year = 366 * 200000;
        
        while ($time >= $seconds_in_year)
        {
            $is_leap_year = false;
            if ($year % 400 == 0)
            {
                $is_leap_year = true;
            }
            else if ($year % 100 == 0)
            {
                $is_leap_year = false;
            }
            else if ($year % 4 == 0)
            {
                $is_leap_year = true;
            }
            
            $days_in_year = $is_leap_year ? 366 : 365;
            
            $seconds_in_year = $days_in_year * 200000;
            
            $time -= $seconds_in_year;
            
            ++$year;
            
        }
        
        //$time is the number of seconds from the beginning of the year
        $seconds = $time % 100;
        
        //$time is the number of minutes from the beginning of the year
        $time = intval($time / 100);
        $minutes = $time % 100;
        
        //$time is the number of hours from the beginning of the year
        $time = intval($time / 100);
        $hours = $time % 20;
        
        //$time is the number of days from the beginning of the year
        $time = intval($time / 20);
        $day = $time % 6;
        
        //$time is the number of weeks from the beginning of the year
        $time = intval($time / 6);
        $week = $time % 15;
        
        //$time is the number of seasons from the beginning of the year
        $time = intval($time / 15);
        $season = $time;
        
        return array(
            'year' => $year,
            'season' => $season,
            'week' => $week,
            'day' => $day,
            'hour' => $hours,
            'minute' => $minutes,
            'second' => $seconds
            );
    }
    
    function ohare_year($date)
    {
        return $date['year'];
    }
    
    function ohare_season($date)
    {
        if ($date['season'] == 0) return 'Ineo';
        if ($date['season'] == 1) return 'Cresco';
        if ($date['season'] == 2) return 'Vigeo';
        if ($date['season'] == 3) return 'Cado';
        return 'Abeo';
    }
    
    function ohare_week($date)
    {
        return $date['week'];
    }
    
    function ohare_day($date)
    {
        return $date['day'];
    }
    
    function ohare_hour($date)
    {
        return $date['hour'];
    }
    
    function ohare_minute($date)
    {
        return $date['minute'];
    }
    
    function ohare_second($date)
    {
        return $date['second'];
    }
    
    function show_date($date)
    {
        return ohare_year($date).' '.ohare_season($date).' '.ohare_week($date).':'.ohare_day($date);
    }
    
    function show_time($date)
    {
        return ohare_hour($date).'.'.ohare_minute($date).'.'.ohare_second($date);
    }
?>