Skip to content

Commit

Permalink
Add the option to choose the name of the months for the format of the…
Browse files Browse the repository at this point in the history
… hijri date #34
  • Loading branch information
alhoqbani committed Jul 27, 2017
1 parent 8abf47c commit 25abc22
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
35 changes: 35 additions & 0 deletions docs/ArDateTime.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,38 @@ echo $arD->arToDayDateTimeString();
// 'الجمعة, شوال 19, 1438 8:21 مساءً'
```

### Set the output of the date format:
```
$arD = ArUtil::date()->arCreate(1438, 10, 19, 2, 15, 30);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 19 شوال 1438 02:21:39 صباحاً'
$arD->setOutputMode(ArDateTime::ALGERIA_AND_TUNIS);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 جويلية 2017 02:15:30 صباحاً'
$arD->setOutputMode(ArDateTime::ARABIC_AND_TRANSLITERATION);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 تموز/يوليو 2017 02:15:30 صباحاً'
$arD->setOutputMode(ArDateTime::ARABIC_MONTH_NAMES);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 تموز 2017 02:15:30 صباحاً'
$arD->setOutputMode(ArDateTime::HIJRI_FORMAT_IN_ENGLISH);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'Friday 19 Shawwal 1438 02:15:30 AM'
$arD->setOutputMode(ArDateTime::MOROCCO_STYLE);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 يوليوز 2017 02:15:30 صباحاً'
$arD->setOutputMode(ArDateTime::LIBYA_STYLE);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 ناصر 1385 02:15:30 صباحاً'
$arD->setOutputMode(ArDateTime::ARABIC_TRANSLITERATION);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 يوليو 2017 02:15:30 صباحاً'
```
29 changes: 26 additions & 3 deletions src/ArUtil/Time/ArDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@
*/
class ArDateTime extends Carbon
{

/**

const HIJRI_FORMAT = 1;
const ARABIC_MONTH_NAMES = 2;
const ARABIC_TRANSLITERATION = 3;
const ARABIC_AND_TRANSLITERATION = 4;
const LIBYA_STYLE = 5;
const ALGERIA_AND_TUNIS = 6;
const MOROCCO_STYLE = 7;
const HIJRI_FORMAT_IN_ENGLISH = 8;

/**
* @var boolean The instance date is modified.
*/
private $isDirty;

private $outputMode = self::HIJRI_FORMAT;

/**
* Create new instance of ArDateTime and set the hijri date to the date provided.
*
Expand Down Expand Up @@ -165,7 +176,7 @@ private static function parseDateFormat($format, $date)
public function arFormat($format)
{
$d = new Date();
$d->setMode($this->outputMode);
return $d->date($format, $this->timestamp);
}

Expand Down Expand Up @@ -218,6 +229,18 @@ public function setTimestamp( $timestamp ) {
return $instance;
}

/**
* Set the output mode from the arFormat method
* @param int $outputMode
*
* @return ArDateTime
*/
public function setOutputMode( int $outputMode ) {
$this->outputMode = $outputMode;

return $this;
}

/**
* Set the instance Hijri date for today.
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/Time/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ArUtil\Tests\Time;

use ArUtil\ArUtil;
use ArUtil\Time\ArDateTime;

class StringsTest extends AbstractTimeTest
{
Expand All @@ -32,6 +33,34 @@ public function it_rerun_hijri_string_date_for_day_only()
$this->assertEquals(19, $arD->arFormat('j'));
}

/** @test */
public function it_rerun_hijri_according_to_outpu_mode()
{
$arD = ArUtil::date()->arCreate(1438, 10, 19, 2, 15, 30);

$arD->setOutputMode(ArDateTime::ALGERIA_AND_TUNIS);
$this->assertEquals('الجمعة 14 جويلية 2017 02:15:30 صباحاً', $arD->arFormat('l dS F Y h:i:s A'));

$arD->setOutputMode(ArDateTime::ARABIC_AND_TRANSLITERATION);
$this->assertEquals('الجمعة 14 تموز/يوليو 2017 02:15:30 صباحاً', $arD->arFormat('l dS F Y h:i:s A'));

$arD->setOutputMode(ArDateTime::ARABIC_MONTH_NAMES);
$this->assertEquals('الجمعة 14 تموز 2017 02:15:30 صباحاً', $arD->arFormat('l dS F Y h:i:s A'));

$arD->setOutputMode(ArDateTime::HIJRI_FORMAT_IN_ENGLISH);
$this->assertEquals('Friday 19 Shawwal 1438 02:15:30 AM', $arD->arFormat('l dS F Y h:i:s A'));

$arD->setOutputMode(ArDateTime::LIBYA_STYLE);
$this->assertEquals('الجمعة 14 ناصر 1385 02:15:30 صباحاً', $arD->arFormat('l dS F Y h:i:s A'));

$arD->setOutputMode(ArDateTime::MOROCCO_STYLE);
$this->assertEquals('الجمعة 14 يوليوز 2017 02:15:30 صباحاً', $arD->arFormat('l dS F Y h:i:s A'));

$arD->setOutputMode(ArDateTime::ARABIC_TRANSLITERATION);
$this->assertEquals('الجمعة 14 يوليو 2017 02:15:30 صباحاً', $arD->arFormat('l dS F Y h:i:s A'));

}

/** @test */
public function it_rerun_hijri_in_date_time_string()
{
Expand Down

0 comments on commit 25abc22

Please sign in to comment.