├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Concerns └── InteractsWithDates.php ├── Facades └── Hijrian.php ├── LaravelHijrian.php └── LaravelHijrianServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-hijrian` will be documented in this file. 4 | 5 | ## v4.2.0 - 2024-03-12 6 | 7 | ### What's Changed 8 | 9 | * Support Laravel 11.x 10 | 11 | **Full Changelog**: https://github.com/mohamedsabil83/laravel-hijrian/compare/v4.1.0...v4.2.0 12 | 13 | ## v4.1.0 - 2024-03-11 14 | 15 | ### v4.1.0 - 2024-03-11 16 | 17 | #### What's Changed 18 | 19 | * Update namespace 20 | 21 | **Full Changelog**: https://github.com/mohamedsabil83/laravel-hijrian/compare/v4.0.0...v4.1.0 22 | 23 | ## v4.0.0 - 2024-03-11 24 | 25 | ### What's Changed 26 | 27 | * Support Laravel 10.x 28 | * Drop supporting Laravel < 10.x 29 | * Drop supporting PHP < 8.1 30 | * Bump dependabot/fetch-metadata from 1.5.1 to 1.6.0 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/16 31 | * Bump actions/checkout from 3 to 4 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/17 32 | * Bump stefanzweifel/git-auto-commit-action from 4 to 5 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/18 33 | * Bump ramsey/composer-install from 2 to 3 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/19 34 | 35 | **Full Changelog**: https://github.com/mohamedsabil83/laravel-hijrian/compare/v3.2.0...v4.0.0 36 | 37 | ## v3.2.0 - 2023-06-09 38 | 39 | ### What's Changed 40 | 41 | - Update DataTime Convert by @AhmedSaidNoreldin in https://github.com/mohamedsabil83/laravel-hijrian/pull/13 42 | - Bump dependabot/fetch-metadata from 1.1.1 to 1.2.0 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/3 43 | - Bump dependabot/fetch-metadata from 1.2.0 to 1.2.1 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/4 44 | - Bump dependabot/fetch-metadata from 1.2.1 to 1.3.0 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/6 45 | - Bump actions/checkout from 2 to 3 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/5 46 | - Bump dependabot/fetch-metadata from 1.3.0 to 1.3.1 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/7 47 | - Bump dependabot/fetch-metadata from 1.3.1 to 1.3.3 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/8 48 | - Bump dependabot/fetch-metadata from 1.3.3 to 1.3.4 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/9 49 | - Bump dependabot/fetch-metadata from 1.3.4 to 1.3.5 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/10 50 | - Bump dependabot/fetch-metadata from 1.3.5 to 1.3.6 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/12 51 | - Bump dependabot/fetch-metadata from 1.3.6 to 1.4.0 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/14 52 | - Bump dependabot/fetch-metadata from 1.4.0 to 1.5.1 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/15 53 | - Bump ramsey/composer-install from 1 to 2 by @dependabot in https://github.com/mohamedsabil83/laravel-hijrian/pull/11 54 | 55 | ### New Contributors 56 | 57 | - @dependabot made their first contribution in https://github.com/mohamedsabil83/laravel-hijrian/pull/3 58 | - @AhmedSaidNoreldin made their first contribution in https://github.com/mohamedsabil83/laravel-hijrian/pull/13 59 | 60 | **Full Changelog**: https://github.com/mohamedsabil83/laravel-hijrian/compare/v3.1.0...v3.2.0 61 | 62 | ## Support Laravel 9 - 2022-02-08 63 | 64 | Support Laravel 9 65 | 66 | **Full Changelog**: https://github.com/mohamedsabil83/laravel-hijrian/compare/v3.0.0...v3.1.0 67 | 68 | ## v3.0.0 - 2021-12-22 69 | 70 | - Refactor using spatie/laravel-package-tools 71 | 72 | ## v2.0.0 - 2020-05-08 73 | 74 | - Add Laravel 7.x compatibility. 75 | - Drop support for Laravel versions before 5.8 76 | 77 | ## v1.1.0 - 2019-08-10 78 | 79 | - Add Laravel v6 support. 80 | - Clean some files. 81 | - Add test unit. 82 | 83 | ## v1.0.0 - 2019-06-16 84 | 85 | - initial release 86 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) MohamedSabil83 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hijri-Gregorian date converter for Laravel 2 | 3 | MohamedSabil83 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/mohamedsabil83/laravel-hijrian.svg?style=flat-square)](https://packagist.org/packages/mohamedsabil83/laravel-hijrian) 6 | [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/mohamedsabil83/laravel-hijrian/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/mohamedsabil83/laravel-hijrian/actions?query=workflow%3Arun-tests+branch%3Amain) 7 | [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/mohamedsabil83/laravel-hijrian/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/mohamedsabil83/laravel-hijrian/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/mohamedsabil83/laravel-hijrian.svg?style=flat-square)](https://packagist.org/packages/mohamedsabil83/laravel-hijrian) 9 | 10 | This is a Hijri-Gregory date converter for Laravel 5.8+. It's return a date instance of **carbon**, so, you can make any process related to it. 11 | 12 | ## Installation 13 | 14 | You can install the package via composer: 15 | 16 | ```bash 17 | composer require mohamedsabil83/laravel-hijrian 18 | ``` 19 | 20 | ## Usage 21 | 22 | Don't forget to add **`use Hijrian;`** at the top of your file. 23 | 24 | ### Get Hijri date 25 | 26 | ```php 27 | Hijrian::hijri() 28 | ``` 29 | 30 | ### Get Gregory date 31 | 32 | ```php 33 | Hijrian::gregory() 34 | ``` 35 | 36 | ### Convert to Hijri 37 | 38 | ```php 39 | Hijrian::hijri($gregory_date) 40 | ``` 41 | 42 | ### Convert to Gregory 43 | 44 | ```php 45 | Hijrian::gregory($hijri_date) 46 | ``` 47 | 48 | ## Testing 49 | 50 | ```bash 51 | composer test 52 | ``` 53 | 54 | ## Changelog 55 | 56 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 57 | 58 | ## Contributing 59 | 60 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 61 | 62 | ## Security Vulnerabilities 63 | 64 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 65 | 66 | ## Credits 67 | 68 | - [MohamedSabil83](https://github.com/mohamedsabil83) 69 | - [All Contributors](../../contributors) 70 | 71 | ## License 72 | 73 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 74 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mohamedsabil83/laravel-hijrian", 3 | "description": "Hijri-Gregorian date converter for Laravel", 4 | "keywords": [ 5 | "Gregorian", 6 | "Gregory", 7 | "Hijri", 8 | "converter", 9 | "date", 10 | "laravel", 11 | "laravel-hijrian" 12 | ], 13 | "homepage": "https://github.com/mohamedsabil83/laravel-hijrian", 14 | "license": "MIT", 15 | "authors": [ 16 | { 17 | "name": "Mohamed Sabil", 18 | "email": "me@mohamedsabil83.com", 19 | "role": "Developer" 20 | } 21 | ], 22 | "require": { 23 | "php": "^8.1", 24 | "illuminate/contracts": "^10.0|^11.0", 25 | "spatie/laravel-package-tools": "^1.14.0" 26 | }, 27 | "require-dev": { 28 | "larastan/larastan": "^2.2", 29 | "laravel/pint": "^1.0", 30 | "nunomaduro/collision": "^7.0|^8.0", 31 | "orchestra/testbench": "^9.5.2", 32 | "pestphp/pest": "^2.0", 33 | "pestphp/pest-plugin-laravel": "^2.0", 34 | "phpstan/extension-installer": "^1.1", 35 | "phpstan/phpstan-deprecation-rules": "^1.0", 36 | "phpstan/phpstan-phpunit": "^1.0", 37 | "phpunit/phpunit": "^10.0|^11.0", 38 | "spatie/laravel-ray": "^1.26" 39 | }, 40 | "autoload": { 41 | "psr-4": { 42 | "MohamedSabil83\\LaravelHijrian\\": "src" 43 | } 44 | }, 45 | "autoload-dev": { 46 | "psr-4": { 47 | "MohamedSabil83\\LaravelHijrian\\Tests\\": "tests" 48 | } 49 | }, 50 | "scripts": { 51 | "post-autoload-dump": "@composer run prepare", 52 | "clear": "@php vendor/bin/testbench package:purge-laravel-hijrian --ansi", 53 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 54 | "build": [ 55 | "@composer run prepare", 56 | "@php vendor/bin/testbench workbench:build --ansi" 57 | ], 58 | "start": [ 59 | "Composer\\Config::disableProcessTimeout", 60 | "@composer run build", 61 | "@php vendor/bin/testbench serve" 62 | ], 63 | "analyse": "vendor/bin/phpstan analyse", 64 | "test": "vendor/bin/pest", 65 | "test-coverage": "vendor/bin/pest --coverage", 66 | "format": "vendor/bin/pint" 67 | }, 68 | "config": { 69 | "sort-packages": true, 70 | "allow-plugins": { 71 | "pestphp/pest-plugin": true, 72 | "phpstan/extension-installer": true 73 | } 74 | }, 75 | "extra": { 76 | "laravel": { 77 | "providers": [ 78 | "MohamedSabil83\\LaravelHijrian\\LaravelHijrianServiceProvider" 79 | ] 80 | } 81 | }, 82 | "minimum-stability": "dev", 83 | "prefer-stable": true 84 | } 85 | -------------------------------------------------------------------------------- /src/Concerns/InteractsWithDates.php: -------------------------------------------------------------------------------- 1 | $mcjdn) { 146 | break; 147 | } 148 | } 149 | 150 | // compute and output the Umm al-Qura calendar date 151 | 152 | $iln = $i + 16260; 153 | $ii = floor(($iln - 1) / 12); 154 | $iy = $ii + 1; 155 | $im = $iln - 12 * $ii; 156 | $id = $mcjdn - self::$UQD[$i - 1] + 1; 157 | 158 | return self::$date->setYear($iy)->setMonth($im)->setDay($id); 159 | } 160 | 161 | private static function gth2($year, $month, $day, $hours, $minutes, $seconds) 162 | { 163 | $year = (int) $year; 164 | $month = (int) $month - 1; // Month start with Zero 165 | $day = (int) $day; 166 | 167 | $y = $year; 168 | $m = $month + 1; 169 | 170 | // append January and February to the previous year (i.e. regard March as 171 | // the first month of the year in order to simplify leapday corrections) 172 | 173 | $y = $m < 3 ? --$y : $y; 174 | $m = $m < 3 ? $m += 12 : $m; 175 | 176 | // determine offset between Julian and Gregorian calendar 177 | 178 | $a = floor($y / 100.); 179 | $jgc = $a - floor($a / 4.) - 2; 180 | 181 | // compute Chronological Julian Day Number (CJDN) 182 | 183 | $cjdn = floor(365.25 * ($y + 4716)) + floor(30.6001 * ($m + 1)) + $day - $jgc - 1524; 184 | 185 | $a = floor(($cjdn - 1867216.25) / 36524.25); 186 | $jgc = $a - floor($a / 4.) + 1; 187 | $b = $cjdn + $jgc + 1524; 188 | $c = floor(($b - 122.1) / 365.25); 189 | $d = floor(365.25 * $c); 190 | $month = floor(($b - $d) / 30.6001); 191 | 192 | // compute Modified Chronological Julian Day Number (MCJDN) 193 | 194 | $mcjdn = $cjdn - 2400000; 195 | 196 | // the MCJDN's of the start of the lunations in the Umm al-Qura calendar are stored in 'islamcalendar_dat.js' 197 | 198 | $count = count(self::$UQD); 199 | for ($i = 0; $i < $count; $i++) { 200 | if (self::$UQD[$i] > $mcjdn) { 201 | break; 202 | } 203 | } 204 | 205 | // compute and output the Umm al-Qura calendar date 206 | 207 | $iln = $i + 16260; 208 | $ii = floor(($iln - 1) / 12); 209 | $iy = $ii + 1; 210 | $im = $iln - 12 * $ii; 211 | $id = $mcjdn - self::$UQD[$i - 1] + 1; 212 | 213 | return self::$date->setYear($iy)->setMonth($im)->setDay($id)->setHours($hours)->setMinutes($minutes)->setSeconds($seconds); 214 | } 215 | 216 | /** 217 | * Convert Hijri to Gregorian. 218 | * 219 | * @param int $year 220 | * @param int $month 221 | * @param int $day 222 | * @return \Carbon\Carbon 223 | */ 224 | private static function htg($year, $month, $day) 225 | { 226 | $year = (int) $year; 227 | $month = (int) $month; 228 | $day = (int) $day; 229 | 230 | $ii = $year - 1; 231 | $iln = ($ii * 12) + 1 + ($month - 1); 232 | $i = $iln - 16260; 233 | $mcjdn = $day + self::$UQD[$i - 1] - 1; 234 | $cjdn = $mcjdn + 2400000; 235 | 236 | return self::jtg($cjdn); 237 | } 238 | 239 | private static function htg2($year, $month, $day, $hours, $minutes, $seconds) 240 | { 241 | $year = (int) $year; 242 | $month = (int) $month; 243 | $day = (int) $day; 244 | $hours = (int) $hours; 245 | $minutes = (int) $minutes; 246 | $seconds = (int) $seconds; 247 | 248 | $ii = $year - 1; 249 | $iln = ($ii * 12) + 1 + ($month - 1); 250 | $i = $iln - 16260; 251 | $mcjdn = $day + self::$UQD[$i - 1] - 1; 252 | $cjdn = $mcjdn + 2400000; 253 | 254 | return self::jtg2($cjdn, $hours, $minutes, $seconds); 255 | } 256 | 257 | /** 258 | * Convert Julian to Gregorian. 259 | * 260 | * @param int $date 261 | * @return \Carbon\Carbon 262 | * 263 | * @source http://keith-wood.name/calendars.html 264 | */ 265 | private static function jtg($date) 266 | { 267 | $z = floor($date + 0.5); 268 | $a = floor(($z - 1867216.25) / 36524.25); 269 | $a = $z + 1 + $a - floor($a / 4); 270 | $b = $a + 1524; 271 | $c = floor(($b - 122.1) / 365.25); 272 | $d = floor(365.25 * $c); 273 | $e = floor(($b - $d) / 30.6001); 274 | $day = $b - $d - floor($e * 30.6001); 275 | $month = $e - ($e > 13.5 ? 13 : 1); 276 | $year = $c - ($month > 2.5 ? 4716 : 4715); 277 | 278 | $year = $year <= 0 ? $year-- : $year; 279 | 280 | return self::$date->setYear($year)->setMonth($month)->setDay($day); 281 | } 282 | 283 | private static function jtg2($date, $hours, $minutes, $seconds) 284 | { 285 | $z = floor($date + 0.5); 286 | $a = floor(($z - 1867216.25) / 36524.25); 287 | $a = $z + 1 + $a - floor($a / 4); 288 | $b = $a + 1524; 289 | $c = floor(($b - 122.1) / 365.25); 290 | $d = floor(365.25 * $c); 291 | $e = floor(($b - $d) / 30.6001); 292 | $day = $b - $d - floor($e * 30.6001); 293 | $month = $e - ($e > 13.5 ? 13 : 1); 294 | $year = $c - ($month > 2.5 ? 4716 : 4715); 295 | 296 | $year = $year <= 0 ? $year-- : $year; 297 | 298 | return self::$date->setYear($year)->setMonth($month)->setDay($day)->setHours($hours)->setMinutes($minutes)->setSeconds($seconds); 299 | } 300 | } 301 | -------------------------------------------------------------------------------- /src/Facades/Hijrian.php: -------------------------------------------------------------------------------- 1 | parse($date)->format('Y-m-d')); 28 | 29 | return self::gth((int) $year, (int) $month, (int) $day); 30 | } 31 | 32 | public static function hijriDateTime($date = null) 33 | { 34 | 35 | $time = date('H:i:s', strtotime($date)); 36 | // $time = str_replace('PM','م', $time); 37 | // $time = str_replace('AM','ص',$time); 38 | $date = date('Y-m-d', strtotime($date)); 39 | 40 | [$hours, $minutes, $seconds] = explode(':', $time); 41 | [$year, $month, $day] = explode('-', self::$date->parse($date)->format('Y-m-d')); 42 | 43 | return self::gth2((int) $year, (int) $month, (int) $day, (int) $hours, (int) $minutes, (int) $seconds); 44 | } 45 | 46 | /** 47 | * Return Gregorian date. 48 | * 49 | * @param string|null $date 50 | * @return \Carbon\Carbon 51 | */ 52 | public static function gregory($date = null) 53 | { 54 | if (empty($date)) { 55 | return self::$date->now(); 56 | } 57 | 58 | [$year, $month, $day] = explode('-', self::$date->parse($date)->format('Y-m-d')); 59 | 60 | return self::htg((int) $year, (int) $month, (int) $day); 61 | } 62 | 63 | public static function gregoryDateTime($date = null) 64 | { 65 | if (empty($date)) { 66 | return self::$date->now(); 67 | } 68 | 69 | // check Arabic Date And Replace AM & PM 70 | $date = str_replace('م', 'PM', $date); 71 | $date = str_replace('ص', 'AM', $date); 72 | 73 | $time = date('G:i:s', strtotime($date)); 74 | $date = date('Y-m-d', strtotime($date)); 75 | 76 | [$hours, $minutes, $seconds] = explode(':', $time); 77 | [$year, $month, $day] = explode('-', self::$date->parse($date)->format('Y-m-d')); 78 | 79 | return self::htg2((int) $year, (int) $month, (int) $day, (int) $hours, (int) $minutes, (int) $seconds); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/LaravelHijrianServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-hijrian'); 14 | } 15 | } 16 | --------------------------------------------------------------------------------