Welcome Visitor, Please Login or Register Now Thursday, September 09, 2010 01:09 AM 
Forums Index > Islamware Announcements > References > Muslim Prayer Times Calculation PHP Class
New Topic   New Reply
Search for:
Author Message
ahmed ahmed's personal page
Administrator Team Member
Posts: 80 Display member's posts
Joined: 07-17-06 12:53 PM
Member Offline
View Member's Profile Visit member's website http:// Send private message
 
Back to top
Muslim Prayer Times Calculation PHP Class Edit Delete Reply with quote Quote
First Post Posted on: 11-01-08 10:14 AM next post first post

Code is attached to this topic.

This class can be used to calculate the time of Muslim prayer time according to the geographic location. It takes the date of the year and the geographic coordinates of a location and returns an array with the local time on which the each the obligatory prayers should occur: Fajr, Zuhr, Asr, Maghrib and Isha .


Code: PHP (HTML)    Select All    Expand All
<?php
// ----------------------------------------------------------------------
// Copyright (C) 2008 by Khaled Al-Shamaa.
// http://www.ar-php.com
// ----------------------------------------------------------------------
// LICENSE

// This program is open source product; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Class Name: Muslim Prayer Times
// Filename:   Salat.class.php
// Original    Author(s): Khaled Al-Sham'aa <khaled.alshamaa@gmail.com>
// Purpose:    The five Islamic prayers are named Fajr, Zuhr, Asr, Maghrib
//             and Isha. The timing of these five prayers varies from place
//             to place and from day to day. It is obligatory for Muslims
//             to perform these prayers at the correct time.
// ----------------------------------------------------------------------
// Source: http://qasweb.org/qasforum/index.php?showtopic=177&st=0
// By: Mohamad Magdy <mohamad_magdy_egy@hotmail.com>
// ----------------------------------------------------------------------

/**
* Muslim Prayer Times

*
* @desc Using this PHP Class you can calculate the time of Muslim prayer 
* according to the geographic location.
*
* @copyright Khaled Al-Shamaa 2008
@link http://www.ar-php.com
@author Khaled Al-Shamaa <khaled.alshamaa@gmail.com>
@package Arabic
*/

class Salat {
    private $year = 1975; // &#1575;&#1604;&#1587;&#1606;&#1577;
    private $month = 8; // &#1575;&#1604;&#1588;&#1607;&#1585;
    private $day = 2; // &#1575;&#1604;&#1610;&#1608;&#1605;
    private $zone = 2; // &#1601;&#1585;&#1602; &#1575;&#1604;&#1578;&#1608;&#1602;&#1610;&#1578; &#1575;&#1604;&#1593;&#1575;&#1604;&#1605;&#1609;
    private $long = 37.15861; // &#1582;&#1591; &#1575;&#1604;&#1591;&#1608;&#1604; &#1575;&#1604;&#1580;&#1594;&#1585;&#1575;&#1601;&#1609; &#1604;&#1604;&#1605;&#1603;&#1575;&#1606;
    private $lat = 36.20278; // &#1582;&#1591; &#1575;&#1604;&#1593;&#1585;&#1590; &#1575;&#1604;&#1580;&#1594;&#1585;&#1575;&#1601;&#1609;
    private $AB2 =  - 0.833333; // &#1586;&#1575;&#1608;&#1610;&#1577; &#1575;&#1604;&#1588;&#1585;&#1608;&#1602; &#1608;&#1575;&#1604;&#1594;&#1585;&#1608;&#1576;
    private $AG2 =  - 18; // &#1586;&#1575;&#1608;&#1610;&#1577; &#1575;&#1604;&#1593;&#1588;&#1575;&#1569;
    private $AJ2 =  - 18; // &#1586;&#1575;&#1608;&#1610;&#1577; &#1575;&#1604;&#1601;&#1580;&#1585;
    private $school = 'Shafi'; // &#1575;&#1604;&#1605;&#1584;&#1607;&#1576;

    /**
     * 
@return TRUE if success, or FALSE if fail
     * @param Integer $d day of date you want to calculate Salat in
     * @param Integer $m month of date you want to calculate Salat in
     * @param Integer $y year (four digits) of date you want to calculate Salat in
     * @desc Setting date of day for Salat calculation
     * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com>
     */
    public function setDate($d = 2, $m = 8, $y = 1975) {
        $flag = true;

        if (is_numeric($y) && $y > 0 && $y < 3000) {
            $this->year = floor($y);
        } else {
            $flag = false;
        }

        if (is_numeric($m) && $m >= 1 && $m <= 12) {
            $this->month = floor($m);
        } else {
            $flag = false;
        }

        if (is_numeric($d) && $d >= 1 && $d <= 31) {
            $this->day = floor($d);
        } else {
            $flag = false;
        }

        return $flag;
    }

    /**
     * 
@return TRUE if success, or FALSE if fail
     * @param Decimal $l1 Longitude of location you want to calculate Salat time in
     * @param Decimal $l2 Latitude of location you want to calculate Salat time in
     * @param Integer $z Time Zone, offset from UTC (see also Greenwich Mean Time)
     * @desc Setting location information for Salat calculation
     * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com>
     */
    public function setLocation($l1 = 37.15861, $l2 = 36.20278, $z = 2) {
        $flag = true;

        if (is_numeric($l1) && $l1 >=  - 180 && $l1 <= 180) {
            $this->long = $l1;
        } else {
            $flag = false;
        }

        if (is_numeric($l2) && $l2 >=  - 180 && $l2 <= 180) {
            $this->lat = $l2;
        } else {
            $flag = false;
        }

        if (is_numeric($z) && $z >=  - 12 && $z <= 12) {
            $this->zone = floor($z);
        } else {
            $flag = false;
        }

        return $flag;
    }

    /**
     * 
@return TRUE if success, or FALSE if fail
     * @param String $sch [Shafi|Hanafi] to define Muslims Salat calculation method
     *               (affect Asr time)
     * @param Decimal $sunriseArc Sun rise arc (default value is -0.833333)
     * @param Decimal $ishaArc Isha arc (default value is -18)
     * @param Decimal $fajrArc Fajr arc (default value is -18)
     * @desc Setting rest of Salat calculation configuration
     * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com>
     */
    public function setConf($sch = 'Shafi', $sunriseArc =  - 0.833333, $ishaArc
        =  - 18, $fajrArc =  - 18) {
        $flag = true;

        $sch = ucfirst($sch);

        if (in_array($sch, array('Shafi', 'Hanafi'))) {
            $this->school = $sch;
        } else {
            $flag = false;
        }

        if (is_numeric($sunriseArc) && $sunriseArc >=  - 180 && $sunriseArc <=
            180) {
            $this->AB2 = $sunriseArc;
        } else {
            $flag = false;
        }

        if (is_numeric($ishaArc) && $ishaArc >=  - 180 && $ishaArc <= 180) {
            $this->AG2 = $ishaArc;
        } else {
            $flag = false;
        }

        if (is_numeric($fajrArc) && $fajrArc >=  - 180 && $fajrArc <= 180) {
            $this->AJ2 = $fajrArc;
        } else {
            $flag = false;
        }

        return $flag;
    }

    /**
     * 
@return Array of Salat times + sun rise in the following format
     *               hh:mm where hh is the hour in local format and 24 mode
     *                     mm is minutes with leading zero to be 2 digits always
     *               Array items is [Fajr, Sunrise, Zuhr, Asr, Maghrib, Isha]
     * @desc Calculate Salat times for the date set in setSalatDate
     *                   methode, and location set in setSalatLocation.
     * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com>
     * @author Mohamad Magdy <mohamad_magdy_egy@hotmail.com>
     * @source http://qasweb.org/qasforum/index.php?showtopic=177&st=0
     */
    public function getPrayTime() {
        $prayTime = array();

        // &#1606;&#1581;&#1587;&#1576; &#1575;&#1604;&#1610;&#1608;&#1605; &#1575;&#1604;&#1580;&#1608;&#1604;&#1610;&#1575;&#1606;&#1609;
        $d = ((367 * $this->year) - (floor((7 / 4) * ($this->year + floor(
            ($this->month + 9) / 12)))) + floor(275 * ($this->month / 9)) +
            $this->day - 730531.5);

        // &#1606;&#1581;&#1587;&#1576; &#1591;&#1608;&#1604; &#1575;&#1604;&#1588;&#1605;&#1587; &#1575;&#1604;&#1608;&#1587;&#1591;&#1609;
        $L = ((280.461 + 0.9856474 * $d) % 360) + ((280.461 + 0.9856474 * $d) -
            (int)(280.461 + 0.9856474 * $d));

        // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1581;&#1589;&#1577; &#1575;&#1604;&#1588;&#1605;&#1587; &#1575;&#1604;&#1608;&#1587;&#1591;&#1609;
        $M = ((357.528 + 0.9856003 * $d) % 360) + ((357.528 + 0.9856003 * $d) -
            (int)(357.528 + 0.9856003 * $d));

        // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1591;&#1608;&#1604; &#1575;&#1604;&#1588;&#1605;&#1587; &#1575;&#1604;&#1576;&#1585;&#1608;&#1580;&#1609;
        $lambda = $L + 1.915 * sin($M * pi() / 180) + 0.02 * sin(2 * $M * pi()
            / 180);

        // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1605;&#1610;&#1604; &#1583;&#1575;&#1574;&#1585;&#1577; &#1575;&#1604;&#1576;&#1585;&#1608;&#1580;
        $obl = 23.439 - 0.0000004 * $d;

        // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1591;&#1604;&#1593; &#1575;&#1604;&#1605;&#1587;&#1578;&#1602;&#1610;&#1605;
        $alpha = atan(cos($obl * pi() / 180) * tan($lambda * pi() / 180)) * 180
            / pi();
        $alpha = $alpha - (360 * floor($alpha / 360));

        // &#1579;&#1605; &#1606;&#1593;&#1583;&#1604; &#1575;&#1604;&#1605;&#1591;&#1604;&#1593; &#1575;&#1604;&#1605;&#1587;&#1578;&#1602;&#1610;&#1605;
        $alpha = $alpha + 90 * ((int)($lambda / 90) - (int)($alpha / 90));

        // &#1606;&#1581;&#1587;&#1576; &#1575;&#1604;&#1586;&#1605;&#1606; &#1575;&#1604;&#1606;&#1580;&#1605;&#1609; &#1576;&#1575;&#1604;&#1583;&#1585;&#1580;&#1575;&#1578; &#1575;&#1604;&#1586;&#1575;&#1608;&#1610;&#1577;
        $ST = ((100.46 + 0.985647352 * $d) % 360) + ((100.46 + 0.985647352 * $d)
            - (int)(100.46 + 0.985647352 * $d));

        // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1605;&#1610;&#1604; &#1575;&#1604;&#1588;&#1605;&#1587; &#1575;&#1604;&#1586;&#1575;&#1608;&#1609;
        $Dec = asin(sin($obl * pi() / 180) * sin($lambda * pi() / 180)) * 180 /
            pi();

        // &#1606;&#1581;&#1587;&#1576; &#1586;&#1608;&#1575;&#1604; &#1575;&#1604;&#1588;&#1605;&#1587; &#1575;&#1604;&#1608;&#1587;&#1591;&#1609;
        if ($alpha > $ST) {
            $noon = (($alpha - $ST) % 360) + (($alpha - $ST) - (int)($alpha -
                $ST));
        } else {
            $noon = (($ST - $alpha) % 360) - (($ST - $alpha) - (int)($ST -
                $alpha));
        }

        // &#1579;&#1605; &#1575;&#1604;&#1586;&#1608;&#1575;&#1604;&#1609; &#1575;&#1604;&#1593;&#1575;&#1604;&#1605;&#1609;
        $un_noon = $noon - $this->long;

        // &#1579;&#1605; &#1575;&#1604;&#1586;&#1608;&#1575;&#1604; &#1575;&#1604;&#1605;&#1581;&#1604;&#1609;
        $local_noon = $un_noon / 15+$this->zone;

        // &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1592;&#1607;&#1585;
        $Dhuhr = $local_noon / 24;
        $Dhuhr_h = (int)($Dhuhr * 24 * 60 / 60);
        $Dhuhr_m = sprintf("d", ($Dhuhr * 24 * 60) % 60);
        $prayTime[2] = "$Dhuhr_h:$Dhuhr_m";

        if ($this->school == 'Shafi') {
            // &#1606;&#1581;&#1587;&#1576; &#1573;&#1585;&#1578;&#1601;&#1575;&#1593; &#1575;&#1604;&#1588;&#1605;&#1587; &#1604;&#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1589;&#1585; &#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1584;&#1607;&#1576; &#1575;&#1604;&#1588;&#1575;&#1601;&#1593;&#1610;
            $T = atan(1+tan(($this->lat - $Dec) * pi() / 180)) * 180 / pi();

            // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1602;&#1608;&#1587; &#1575;&#1604;&#1583;&#1575;&#1574;&#1585; &#1571;&#1609; &#1575;&#1604;&#1608;&#1602;&#1578; &#1575;&#1604;&#1605;&#1578;&#1576;&#1602;&#1609; &#1605;&#1606; &#1608;&#1602;&#1578; &#1575;&#1604;&#1592;&#1607;&#1585; &#1581;&#1578;&#1609; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1589;&#1585; &#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1584;&#1607;&#1576; &#1575;&#1604;&#1588;&#1575;&#1601;&#1593;&#1610;
            $V = acos((sin((90-$T) * pi() / 180) - sin($Dec * pi() / 180) * sin
                ($this->lat * pi() / 180)) / (cos($Dec * pi() / 180) * cos
                ($this->lat * pi() / 180))) * 180 / pi() / 15;

            // &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1589;&#1585; &#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1584;&#1607;&#1576; &#1575;&#1604;&#1588;&#1575;&#1601;&#1593;&#1610;
            $X = $local_noon + $V;
            $SAsr = $Dhuhr + $V / 24;
            $SAsr_h = (int)($SAsr * 24 * 60 / 60);
            $SAsr_m = sprintf("d", ($SAsr * 24 * 60) % 60);
            $prayTime[3] = "$SAsr_h:$SAsr_m";
        } else {
            // &#1606;&#1581;&#1587;&#1576; &#1573;&#1585;&#1578;&#1601;&#1575;&#1593; &#1575;&#1604;&#1588;&#1605;&#1587; &#1604;&#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1589;&#1585; &#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1584;&#1607;&#1576; &#1575;&#1604;&#1581;&#1606;&#1601;&#1610;
            $U = atan(2+tan(($this->lat - $Dec) * pi() / 180)) * 180 / pi();

            // &#1579;&#1605; &#1606;&#1581;&#1587;&#1576; &#1602;&#1608;&#1587; &#1575;&#1604;&#1583;&#1575;&#1574;&#1585; &#1571;&#1609; &#1575;&#1604;&#1608;&#1602;&#1578; &#1575;&#1604;&#1605;&#1578;&#1576;&#1602;&#1609; &#1605;&#1606; &#1608;&#1602;&#1578; &#1575;&#1604;&#1592;&#1607;&#1585; &#1581;&#1578;&#1609; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1589;&#1585; &#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1584;&#1607;&#1576; &#1575;&#1604;&#1581;&#1606;&#1601;&#1610;
            $W = acos((sin((90-$U) * pi() / 180) - sin($Dec * pi() / 180) * sin
                ($this->lat * pi() / 180)) / (cos($Dec * pi() / 180) * cos
                ($this->lat * pi() / 180))) * 180 / pi() / 15;

            // &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1589;&#1585; &#1581;&#1587;&#1576; &#1575;&#1604;&#1605;&#1584;&#1607;&#1576; &#1575;&#1604;&#1581;&#1606;&#1601;&#1610;
            $Z = $local_noon + $W;
            $HAsr = $Z / 24;
            $HAsr_h = (int)($HAsr * 24 * 60 / 60);
            $HAsr_m = sprintf("d", ($HAsr * 24 * 60) % 60);
            $prayTime[3] = "$HAsr_h:$HAsr_m";
        }

        // &#1606;&#1581;&#1587;&#1576; &#1606;&#1589;&#1601; &#1602;&#1608;&#1587; &#1575;&#1604;&#1606;&#1607;&#1575;&#1585;
        $AB = acos((SIN($this->AB2 * pi() / 180) - sin($Dec * pi() / 180) * sin
            ($this->lat * pi() / 180)) / (cos($Dec * pi() / 180) * cos($this
            ->lat * pi() / 180))) * 180 / pi();

        // &#1608;&#1602;&#1578; &#1575;&#1604;&#1588;&#1585;&#1608;&#1602;
        $AC = $local_noon - $AB / 15;
        $Sunrise = $AC / 24;
        $Sunrise_h = (int)($Sunrise * 24 * 60 / 60);
        $Sunrise_m = sprintf("d", ($Sunrise * 24 * 60) % 60);
        $prayTime[1] = "$Sunrise_h:$Sunrise_m";

        // &#1608;&#1602;&#1578; &#1575;&#1604;&#1594;&#1585;&#1608;&#1576;
        $AE = $local_noon + $AB / 15;
        $Sunset = $AE / 24;
        $Sunset_h = (int)($Sunset * 24 * 60 / 60);
        $Sunset_m = sprintf("d", ($Sunset * 24 * 60) % 60);
        $prayTime[4] = "$Sunset_h:$Sunset_m";

        // &#1606;&#1581;&#1587;&#1576; &#1601;&#1590;&#1604; &#1575;&#1604;&#1583;&#1575;&#1574;&#1585; &#1608;&#1607;&#1608; &#1575;&#1604;&#1608;&#1602;&#1578; &#1575;&#1604;&#1605;&#1578;&#1576;&#1602;&#1609; &#1605;&#1606; &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1592;&#1607;&#1585; &#1573;&#1604;&#1609; &#1608;&#1602;&#1578; &#1575;&#1604;&#1593;&#1588;&#1575;&#1569;
        $AG = acos((sin($this->AG2 * pi() / 180) - sin($Dec * pi() / 180) * sin
            ($this->lat * pi() / 180)) / (cos($Dec * pi() / 180) * cos($this
            ->lat * pi() / 180))) * 180 / pi();

        // &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1593;&#1588;&#1575;&#1569;
        $AH = $local_noon + ($AG / 15);
        $Isha = $AH / 24;
        $Isha_h = (int)($Isha * 24 * 60 / 60);
        $Isha_m = sprintf("d", ($Isha * 24 * 60) % 60);
        $prayTime[5] = "$Isha_h:$Isha_m";

        // &#1606;&#1581;&#1587;&#1576; &#1601;&#1590;&#1604; &#1583;&#1575;&#1574;&#1585; &#1575;&#1604;&#1601;&#1580;&#1585; &#1608;&#1607;&#1608; &#1575;&#1604;&#1608;&#1602;&#1578; &#1575;&#1604;&#1605;&#1578;&#1576;&#1602;&#1609; &#1605;&#1606; &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1601;&#1580;&#1585; &#1581;&#1578;&#1609; &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1592;&#1607;&#1585;
        $AJ = acos((sin($this->AJ2 * pi() / 180) - sin($Dec * pi() / 180) * sin
            ($this->lat * pi() / 180)) / (cos($Dec * pi() / 180) * cos($this
            ->lat * pi() / 180))) * 180 / pi();

        // &#1608;&#1602;&#1578; &#1589;&#1604;&#1575;&#1577; &#1575;&#1604;&#1601;&#1580;&#1585;
        $AK = $local_noon - $AJ / 15;
        $Fajr = $AK / 24;
        $Fajr_h = (int)($Fajr * 24 * 60 / 60);
        $Fajr_m = sprintf("d", ($Fajr * 24 * 60) % 60);
        $prayTime[0] = "$Fajr_h:$Fajr_m";

        return $prayTime;
    }
}

?>



-----------------------
Ahmed ELsheshtawy
IslamWare CEO
www.islamware.com
-----------------------
-----------------------

File
ahmed ahmed's personal page
Administrator Team Member
Posts: 80 Display member's posts
Joined: 07-17-06 12:53 PM
Member Offline
View Member's Profile Visit member's website http:// Send private message
 
Back to top
attached Edit Delete Reply with quote Quote
Reply #: 1 Posted on: 01-18-09 12:23 PM last post previous post
salat php class is attached

-----------------------
Ahmed ELsheshtawy
IslamWare CEO
www.islamware.com
-----------------------
-----------------------

File
Page 1 of 1
Go to page:

New Topic   New Reply Mark Unread
Jump to:  
Delete   Move     Lock   +Favorits   +Notify   Print