<?php

/**
* Description: Abschätzung(sic!) von Sonnenauf- und Untergang
* Author: Jörg Reinholz, fastix Webdesign & Consult, Kassel (Germany) www.fastix.org
* Version 1.0
**/
error_reporting(E_ALL);

class FtxSonne {

    protected $lat;
    protected $long;
    protected $ort;
    protected $timeZone = 'Europe/Berlin';
    protected $timeFormat = 'H:i';
    protected $year = 1970;
    protected $month = 1;
    protected $day = 1;
    protected $dto;
    protected $tsSunrice;
    protected $tsSunset;
    protected $zenith=90.833333333333333333333;

    public function __construct( $lat=false, $long=false, $timeZone=false ) {
        $this -> setOrt ('Kassel');
        $this -> setTimeZone( $timeZone );
        
    }

    public function setOrt ($ort='Kassel') {
        $ort = strtoupper(trim($ort));
        switch ($ort) {
            case 'KASSEL':                      /* Mitte Deutschlands */
                $this ->  lat = 51.3127114;
                $this -> long = 9.4797461;
                $this -> ort  = 'Kassel';
                break;
            case 'GOERLITZ':                    /* Osten Deutschlands */
                $this ->  lat = 51.1666667;
                $this -> long = 14.9833333;
                $this -> ort  = 'Görlitz';
                break;
            case 'ISENBRUCH':                   /* Westen Deutschlands */
                $this ->  lat = 51.051109;
                $this -> long = 5.866316;
                $this -> ort  = 'Isenbruch';
                break;
            case 'OBERSTDORF':                   /* Süden Deutschlands */
                $this ->  lat = 47.49;
                $this -> long = 10.2833333;
                $this -> ort  = 'Oberstdorf';
                break;
            case 'FLENSBURG':                   /* Norden Deutschlands */
                $this ->  lat = 54.7833333;
                $this -> long = 9.4333333;
                $this -> ort  = 'Flensburg';
                break;
            default: # Kassel
                $this ->  lat = 51.3127114;
                $this -> long = 9.4797461;
                $this -> ort  = 'Default';
                break;
        }
        $this -> calcSun();
    }

    public function getOrt () {
        return $this -> ort;
    }

    public function getLat () {
        return $this -> lat;
    }

    public function getLong () {
        return $this -> long;
    }

    public function setTimeZone( $timeZone=false ) {
        if ( $timeZone == $this -> timeZone ) {
            return true;
        } elseif ( $timeZone ) {
            if ( date_default_timezone_set( $timeZone ) ) {
                $this -> timeZone = $timeZone;
                $this -> calcSun();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function setLong( $long=false ) {
        if ( $long < 180 && $long > -180 ) {
            $this -> long = $long;
            $this -> calcSun();
            return true;
        } else {
            return false;
        }
    }

    public function setLat( $lat=false ) {
        if ( $lat <= 90 && $lat >= -90 ) {
            $this -> long = $lat;
            $this -> calcSun();
            return true;
        } else {
            return false;
        }
    }

    public function setLatAndLong( $lat=false, $long=false ) {
        return ( $this -> setLat($lat) && $this->setLong($long) );
    }


    public function setDate( $day, $month, $year ) {
        if (! $day)   { $day   = $this -> day;   }
        if (! $month) { $month = $this -> month; }
        if (! $year)  { $year =  $this -> year;  }
        if ( $dto = mktime( 12, 0, 0, $month, $day, $year) ) {
            $this -> day   = $day;
            $this -> month = $month;
            $this -> year  = $year;
            $this -> dto = $dto;
            $this -> calcSun();
        }
        return $dto;
    }

    public function setDay( $day )     { $this -> setDate( $day, $this -> month, $this -> year );   }

    public function setMonth( $month ) { $this -> setDate( $this -> day, $month, $this -> year ); }

    public function setYear( $year )   { $this -> setDate( $this -> day, $this -> month, $year ); }


    public function calcSun() {
        $this ->tsSunrice =  date_sunrise( $this -> dto, SUNFUNCS_RET_TIMESTAMP, $this -> lat, $this -> long, $this -> zenith );
        $this ->tsSunset  =  date_sunset(  $this -> dto, SUNFUNCS_RET_TIMESTAMP, $this -> lat, $this -> long, $this -> zenith );
    }

    public function getSunrice( $format=false ) {
        if ( ! $format ) { $format = $this -> timeFormat; }
        if ( $this -> dto ) {
			#$lt = localtime ( $this -> tsSunrice, true );
			#return $lt['tm_hour'] . ':'  . $lt['tm_min'];
            return date( $format, $this -> tsSunrice );
            
        } else {
            return false;
        }
    }

    public function getSunset( $format = false ) {
        if ( ! $format ) { $format = $this -> timeFormat; }
        if ( $this -> dto ) {
			#$lt = localtime ( $this -> tsSunset, true );
			#return $lt['tm_hour'] . ':'  . $lt['tm_min'];
            return date( $format, $this -> tsSunset );
        } else {
            return false;
        }
    }
}


/**** Test: ****
$sonne=new FtxSonne;
$sonne -> setOrt('Kassel');
echo $sonne -> getOrt(), " ( ", $sonne -> getLat(), " / " , $sonne -> getLong(), " )\n";
$sonne -> setDate(1,1,2016);
for ($i=1; $i<=366; $i++) {
    $sonne -> setDay($i);
    echo date('Y-m-d', mktime(0,0,0, 1,$i,2016)) .' : '. $sonne -> getSunrice('H:i:s') . ' bis ' .  $sonne -> getSunset('H:i:s') . "\n\n";
}
#*/