PHP code example of muslims-community / prayer-times-calculation

1. Go to this page and download the library: Download muslims-community/prayer-times-calculation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

muslims-community / prayer-times-calculation example snippets


'providers' => [
    // ...
    MuslimsCommunity\PrayerTimes\PrayerTimesServiceProvider::class,
],

'aliases' => [
    // ...
    'PrayerTimes' => MuslimsCommunity\PrayerTimes\Facades\PrayerTimes::class,
],

use DateTime;
use MuslimsCommunity\PrayerTimes\PrayerTimesSDK;
use MuslimsCommunity\PrayerTimes\Data\CalculationOptions;
use MuslimsCommunity\PrayerTimes\Enums\CalculationMethod;
use MuslimsCommunity\PrayerTimes\Enums\AsrJurisdiction;

// Create calculation options
$options = new CalculationOptions(
    CalculationMethod::MWL,
    AsrJurisdiction::STANDARD
);

// Initialize the SDK
$prayerTimes = new PrayerTimesSDK(
    21.4225,  // Latitude (Makkah)
    39.8262,  // Longitude (Makkah)
    new DateTime(),  // Date
    3.0,      // Timezone offset (UTC+3)
    $options
);

// Get prayer times
$times = $prayerTimes->getTimes();

// Display prayer times
echo "Prayer Times for Makkah:\n";
echo "Fajr: " . $times->fajr . "\n";
echo "Sunrise: " . $times->sunrise . "\n";
echo "Dhuhr: " . $times->dhuhr . "\n";
echo "Asr: " . $times->asr . "\n";
echo "Maghrib: " . $times->maghrib . "\n";
echo "Isha: " . $times->isha . "\n";

// Convert to array for JSON API responses
$timesArray = $times->toArray();

use MuslimsCommunity\PrayerTimes\Facades\PrayerTimes;
use MuslimsCommunity\PrayerTimes\Enums\CalculationMethod;
use MuslimsCommunity\PrayerTimes\Enums\AsrJurisdiction;

// Using the facade with parameters
$times = PrayerTimes::calculate(
    latitude: 21.4225,
    longitude: 39.8262,
    date: new DateTime('2023-10-15'),
    timezone: 3.0,
    method: CalculationMethod::MWL,
    asrJurisdiction: AsrJurisdiction::STANDARD
);

// Using the facade with configuration
$times = PrayerTimes::calculateFromConfig(
    latitude: 21.4225,
    longitude: 39.8262
);

// Convert to array
$timesArray = $times->toArray();

use MuslimsCommunity\PrayerTimes\PrayerTimesManager;

class PrayerController extends Controller
{
    public function __construct(
        private PrayerTimesManager $prayerTimesManager
    ) {}

    public function getTimes(Request $request)
    {
        $times = $this->prayerTimesManager->calculate(
            $request->latitude,
            $request->longitude
        );

        return response()->json($times->toArray());
    }
}

use MuslimsCommunity\PrayerTimes\Data\CalculationOptions;
use MuslimsCommunity\PrayerTimes\Enums\CalculationMethod;
use MuslimsCommunity\PrayerTimes\Enums\AsrJurisdiction;

$options = new CalculationOptions(
    CalculationMethod::CUSTOM,
    AsrJurisdiction::HANAFI,
    18.5,  // Custom Fajr angle
    17.5   // Custom Isha angle
);

$prayerTimes = new PrayerTimesSDK(
    33.6844,  // Latitude
    73.0479,  // Longitude
    new DateTime(),
    5.0,      // UTC+5
    $options
);

$times = $prayerTimes->getTimes();

return [
    'method' => env('PRAYER_TIMES_METHOD', 'MWL'),
    'asr_jurisdiction' => env('PRAYER_TIMES_ASR_JURISDICTION', 'Standard'),
    'timezone' => env('PRAYER_TIMES_TIMEZONE', null),
    'fajr_angle' => env('PRAYER_TIMES_FAJR_ANGLE', null),
    'isha_angle' => env('PRAYER_TIMES_ISHA_ANGLE', null),
];