Skip to content

Commit 1c17d84

Browse files
authored
Merge pull request #155 from hos3ein/patch-1
Add Converter trait
2 parents d497946 + 186e05b commit 1c17d84

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

src/Converter.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Morilog\Jalali;
4+
5+
use Carbon\Exceptions\UnitException;
6+
use Date;
7+
8+
/**
9+
* Trait Converter.
10+
*
11+
* Change date into different string formats and types and
12+
* handle the string cast.
13+
*/
14+
trait Converter
15+
{
16+
17+
/**
18+
* Format the instance as date
19+
*
20+
* @return string
21+
*/
22+
public function toDateString()
23+
{
24+
return $this->format("Y/m/d");
25+
}
26+
27+
/**
28+
* Format the instance as a readable date
29+
*
30+
* @return string
31+
*/
32+
public function toFormattedDateString()
33+
{
34+
return $this->format('j F Y');
35+
}
36+
37+
/**
38+
* Format the instance with the day, and a readable date
39+
*
40+
* @return string
41+
*/
42+
public function toFormattedDayDateString(): string
43+
{
44+
return $this->format('l j F Y');
45+
}
46+
47+
/**
48+
* Format the instance as time
49+
*
50+
* @param string $unitPrecision
51+
*
52+
* @return string
53+
*/
54+
public function toTimeString($unitPrecision = 'second')
55+
{
56+
return $this->format(static::getTimeFormatByPrecision($unitPrecision));
57+
}
58+
59+
/**
60+
* Format the instance as date and time
61+
*
62+
* @param string $unitPrecision
63+
*
64+
* @return string
65+
*/
66+
public function toDateTimeString($unitPrecision = 'second')
67+
{
68+
return $this->format('Y/m/d ' . static::getTimeFormatByPrecision($unitPrecision));
69+
}
70+
71+
/**
72+
* Return a format from H:i to H:i:s.u according to given unit precision.
73+
*
74+
* @param string $unitPrecision "minute", "second", "millisecond" or "microsecond"
75+
*
76+
* @return string
77+
*/
78+
public static function getTimeFormatByPrecision($unitPrecision)
79+
{
80+
switch (Date::singularUnit($unitPrecision)) {
81+
case 'minute':
82+
return 'H:i';
83+
case 'second':
84+
return 'H:i:s';
85+
case 'm':
86+
case 'millisecond':
87+
return 'H:i:s.v';
88+
case 'µ':
89+
case 'microsecond':
90+
return 'H:i:s.u';
91+
}
92+
93+
throw new UnitException('Precision unit expected among: minute, second, millisecond and microsecond.');
94+
}
95+
96+
/**
97+
* Format the instance as date and time T-separated with no timezone
98+
* echo Jalalian::now()->toDateTimeLocalString('minute'); // You can specify precision among: minute, second, millisecond and microsecond
99+
* ```
100+
*
101+
* @param string $unitPrecision
102+
*
103+
* @return string
104+
*/
105+
public function toDateTimeLocalString($unitPrecision = 'second')
106+
{
107+
return $this->format('Y-m-d\T' . static::getTimeFormatByPrecision($unitPrecision));
108+
}
109+
110+
/**
111+
* Format the instance with day, date and time
112+
*
113+
* @return string
114+
*/
115+
public function toDayDateTimeString()
116+
{
117+
return $this->format('l j F Y g:i A');
118+
}
119+
120+
}

src/Jalalian.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class Jalalian
99
{
10+
use Converter;
11+
1012
/**
1113
* @var int
1214
*/

0 commit comments

Comments
 (0)