Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArDateTime #28

Merged
merged 6 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/ArDateTime.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@ echo $arD->arToDateString(); // 1985-06-14
In addition to the methods available from `Carbon`,
`ArDateTime` provides some methods to deal with Hijri Date:


#### Create methods:
```
$arD = ArUtil::date()->arCreate($arYear, $arMonth, $arDay, $hour, $minute, $second, $tz);
$arD = ArUtil::date()->arCreateFromDate(1405, 8, 10);
$arD = ArUtil::date()->arCreateFromDateString('1438-10-19', 'Asia/Riyadh');
$arD = ArUtil::date()->arCreateFromDateTimeString('1438-10-19 4:30:45');
$arD = ArUtil::date()->arCreateFromFormat('Y/m/d', '1430/10/01');

```


#### Getters methods:
```

echo sprintf('Toady is AH %s', ArUtil::date()->arToDateString());
// Toady is AH 1438-10-19

echo sprintf('Toady is %s', ArUtil::date()->ToDateString());
// Toady is 2017-07-14

echo sprintf('تاريخ اليوم %sهـ', ArUtil::date()->arFormat('Y/m/d'));
// تاريخ اليوم 1438/10/19هـ

$arD = ArUtil::date()->arCreate(1438, 10, 19, 02, 21, 39);

Expand Down
78 changes: 78 additions & 0 deletions src/ArUtil/Time/ArDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,62 @@ public static function arCreateFromDate(
return $instance;
}

/**
* Create new instance from DateString format: 1438-10-15
*
* @param string $dateString Hijri date in Y-m-d format
* @param \DateTimeZone|string|null $tz
* @param int|null $correction Date conversion factor (+1, +2, -1, -2)
*
* @return \ArUtil\Time\ArDateTime
*/
public static function arCreateFromDateString($dateString, $tz = null, $correction = null)
{
$arDate = date_parse($dateString);

return self::arCreateFromDate($arDate['year'], $arDate['month'], $arDate['day'], $tz, $correction);
}

/**
* Create new instance from DateTimeString format: 1438-10-19 4:30:45
*
* @param string $dateTimeString Hijri date in Y-m-d H:i:s format
* @param \DateTimeZone|string|null $tz
* @param int|null $correction Date conversion factor (+1, +2, -1, -2)
*
* @return \ArUtil\Time\ArDateTime
*/
public static function arCreateFromDateTimeString($dateTimeString, $tz = null, $correction = null)
{
$arDate = date_parse($dateTimeString);

return self::arCreate(
$arDate['year'], $arDate['month'], $arDate['day'], $arDate['hour'], $arDate['minute'], $arDate['second'],
$tz, $correction
);
}

/**
* Create new instance from Hijri date in the format provided.
*
* @param string $format
* @param string $date
* @param \DateTimeZone|string|null $tz
* @param int|null $correction Date conversion factor (+1, +2, -1, -2)
*
* @return \ArUtil\Time\ArDateTime
*/
public static function arCreateFromFormat($format, $date, $tz = null, $correction = null)
{
$arDate = self::parseDateFormat($format, $date);

return self::arCreate($arDate['arYear'], $arDate['arMonth'], $arDate['arDay'], $arDate['hour'],
$arDate['minute'],
$arDate['second'],
$tz, $correction);
}


/**
* ArDateTime constructor, Instantiate new instance and set Today's date in Hijri.
*
Expand All @@ -85,9 +141,31 @@ public function __construct($time = null, $tz = null)
parent::__construct($time, $tz);
}

/**
* Parse the Hijri date according to the format provided.
* @param $format
* @param $date
*
* @return array Parsed date
*/
private static function parseDateFormat($format, $date)
{
$arDate = date_parse_from_format($format, $date);

return [
'arYear' => ($arDate['year'] != false) ? $arDate['year'] : null,
'arMonth' => ($arDate['month'] != false) ? $arDate['month'] : null,
'arDay' => ($arDate['day'] != false) ? $arDate['day'] : null,
'hour' => ($arDate['hour'] != false) ? $arDate['hour'] : null,
'minute' => ($arDate['minute'] != false) ? $arDate['minute'] : null,
'second' => ($arDate['second'] != false) ? $arDate['second'] : null,
];
}

public function arFormat($format)
{
$d = new Date();

return $d->date($format, $this->timestamp);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/Time/CreateFromFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace ArUtil\Tests\Time;

use ArUtil\ArUtil;

class CreateFromFormatTest extends AbstractTimeTest
{

/** @test */
public function it_create_from_date_string()
{
$arD = ArUtil::date()->arCreateFromDateString('1438-10-19');
$this->assertArDateTime($arD, 1438, 10, 19);
$this->assertCarbon($arD, 2017, 7, 14);
}

/** @test */
public function it_create_from_date_string_with_time_zone()
{
$arD = ArUtil::date()->arCreateFromDateString('1438-10-19', 'Asia/Riyadh');
$this->assertEquals('Asia/Riyadh', $arD->timezoneName);
}

/** @test */
public function it_create_from_date_time_string()
{
$arD = ArUtil::date()->arCreateFromDateTimeString('1438-10-19 4:30:45');
$this->assertArDateTime($arD, 1438, 10, 19, 4, 30, 45);
$this->assertCarbon($arD, 2017, 7, 14, 4, 30, 45);
}

/** @test */
public function it_create_from_date_time_string_with_time_zone()
{
$arD = ArUtil::date()->arCreateFromDateTimeString('1438-10-19 4:30:45', 'Asia/Riyadh');
$this->assertEquals('Asia/Riyadh', $arD->timezoneName);
}

/** @test */
public function it_create_from_format()
{
$arD = ArUtil::date()->arCreateFromFormat('Y/m/d', '1430/10/01');
$this->assertArDateTime($arD, 1430, 10, 1);
$this->assertCarbon($arD, 2009, 9, 21);
}
}