1+ <?php namespace Miladr \Jalali ;
2+
3+ /**
4+ * A LaravelPHP helper class for working w/ jalali dates.
5+ * by Sallar Kaboli <[email protected] > 6+ *
7+ *
8+ * Based on Laravel-Date bundle
9+ * by Scott Travis <[email protected] > 10+ * http://github.com/swt83/laravel-date
11+ *
12+ *
13+ * @package jDate
14+ * @author Sallar Kaboli <[email protected] > 15+ * @link http://
16+ * @basedon http://github.com/swt83/laravel-date
17+ * @license MIT License
18+ */
19+
20+ class jDate
21+ {
22+ protected $ time ;
23+
24+ protected $ formats = array (
25+ 'datetime ' => '%Y-%m-%d %H:%M:%S ' ,
26+ 'date ' => '%Y-%m-%d ' ,
27+ 'time ' => '%H:%M:%S ' ,
28+ );
29+
30+ public static function forge ($ str = null )
31+ {
32+ $ class = __CLASS__ ;
33+ return new $ class ($ str );
34+ }
35+
36+ public function __construct ($ str = null )
37+ {
38+ if ($ str === null ){
39+ $ this ->time = time ();
40+ }
41+ else
42+ {
43+ if (is_numeric ($ str )){
44+ $ this ->time = $ str ;
45+ }
46+ else
47+ {
48+ $ time = strtotime ($ str );
49+
50+ if (!$ time ){
51+ $ this ->time = false ;
52+ }
53+ else {
54+ $ this ->time = $ time ;
55+ }
56+ }
57+ }
58+ }
59+
60+ public function time ()
61+ {
62+ return $ this ->time ;
63+ }
64+
65+ public function format ($ str )
66+ {
67+ // convert alias string
68+ if (in_array ($ str , array_keys ($ this ->formats ))){
69+ $ str = $ this ->formats [$ str ];
70+ }
71+
72+ // if valid unix timestamp...
73+ if ($ this ->time !== false ){
74+ return jDateTime::strftime ($ str , $ this ->time );
75+ }
76+ else {
77+ return false ;
78+ }
79+ }
80+
81+ public function reforge ($ str )
82+ {
83+ if ($ this ->time !== false )
84+ {
85+ // amend the time
86+ $ time = strtotime ($ str , $ this ->time );
87+
88+ // if conversion fails...
89+ if (!$ time ){
90+ // set time as false
91+ $ this ->time = false ;
92+ }
93+ else {
94+ // accept time value
95+ $ this ->time = $ time ;
96+ }
97+ }
98+
99+ return $ this ;
100+ }
101+
102+ public function ago ()
103+ {
104+ $ now = time ();
105+ $ time = $ this ->time ();
106+
107+ // catch error
108+ if (!$ time ) return false ;
109+
110+ // build period and length arrays
111+ $ periods = array ('ثانیه ' , 'دقیقه ' , 'ساعت ' , 'روز ' , 'هفته ' , 'ماه ' , 'سال ' , 'قرن ' );
112+ $ lengths = array (60 , 60 , 24 , 7 , 4.35 , 12 , 10 );
113+
114+ // get difference
115+ $ difference = $ now - $ time ;
116+
117+ // set descriptor
118+ if ($ difference < 0 )
119+ {
120+ $ difference = abs ($ difference ); // absolute value
121+ $ negative = true ;
122+ }
123+
124+ // do math
125+ for ($ j = 0 ; $ difference >= $ lengths [$ j ] and $ j < count ($ lengths )-1 ; $ j ++){
126+ $ difference /= $ lengths [$ j ];
127+ }
128+
129+ // round difference
130+ $ difference = intval (round ($ difference ));
131+
132+ // return
133+ return number_format ($ difference ).' ' .$ periods [$ j ].' ' .(isset ($ negative ) ? '' : 'پیش ' );
134+ }
135+
136+ public function until ()
137+ {
138+ return $ this ->ago ();
139+ }
140+ }
0 commit comments