11<?php
22
3- $ timezone_abbreviation = isset ( $ argv [ 2 ]) ? $ argv [ 2 ] : ' PDT ' ;
3+ include ' vendor/autoload.php ' ;
44
5- date_default_timezone_set ( timezone_name_from_abbr ( $ timezone_abbreviation )) ;
5+ use Alfred \ Workflows \ Workflow ;
66
7- $ icon = "icon.icns " ;
7+ $ workflow = new Workflow ();
8+ $ timezoneAbbreviation = isset ($ argv [2 ]) ? $ argv [2 ] : 'PDT ' ;
89
9- require ( 'workflows.php ' ); // by David Ferguson
10- $ wf = new Workflows ();
10+ date_default_timezone_set (timezone_name_from_abbr ($ timezoneAbbreviation ));
1111
1212$ dates = explode (" to " , trim (@$ argv [1 ])); // split arguments by " to "
1313
14- for ($ i =0 ; $ i < sizeof ($ dates ); $ i ++)
15- {
16- $ dates [$ i ] = str_replace ('- ' , '/ ' , $ dates [$ i ]);
17- }
18-
14+ array_map (static function ($ dateString ) {
15+ return str_replace ('- ' , '/ ' , $ dateString );
16+ }, $ dates );
1917
2018/*
2119 * use the current date and time in lieu of a second argument
2220 */
2321
24- $ date2 = isset ($ dates [1 ]) ? new DateTime ($ dates [1 ]) : new DateTime ();
25- $ date1 = new DateTime ($ dates [0 ]);
22+ try {
23+ $ date2 = isset ($ dates [1 ]) ? new DateTime ($ dates [1 ]) : new DateTime ();
24+ $ date1 = new DateTime ($ dates [0 ]);
25+ } catch (Exception $ e ) {
26+ return ;
27+ }
2628
2729$ diff = $ date1 ->diff ($ date2 );
2830
2931// http://www.php.net/manual/en/dateinterval.format.php
3032
31- $ minutes = intval ( $ diff ->format ('%i ' ) );
32- $ hours = intval ( $ diff ->format ('%h ' ) );
33- $ days = intval ( $ diff ->format ('%d ' ) );
34- $ total_days = intval ( $ diff ->format ('%a ' ) );
35- $ total_hours = $ total_days* 24 ;
36- $ total_minutes = $ total_hours* 60 ;
37- $ weeks = intval ( intval ( $ diff ->format ('%a ' ))/ 7 ) ;
38- $ business_weeks = intval ( intval ( $ diff ->format ('%a ' ))/ 5 ) ;
39- $ months = intval ( $ diff ->format ('%m ' ) );
40- $ years = intval ( $ diff ->format ('%y ' ) );
33+ $ minutes = ( int ) $ diff ->format ('%i ' );
34+ $ hours = ( int ) $ diff ->format ('%h ' );
35+ $ days = ( int ) $ diff ->format ('%d ' );
36+ $ total_days = ( int ) $ diff ->format ('%a ' );
37+ $ total_hours = $ total_days * 24 ;
38+ $ total_minutes = $ total_hours * 60 ;
39+ $ weeks = ( int ) $ diff ->format ('%a ' ) / 7 ;
40+ $ business_weeks = ( int ) $ diff ->format ('%a ' ) / 5 ;
41+ $ months = ( int ) $ diff ->format ('%m ' );
42+ $ years = ( int ) $ diff ->format ('%y ' );
4143$ sign = $ diff ->format ('%R ' );
4244
43- if ($ total_days > 1 )
44- {
45+ if ($ total_days > 1 ) {
4546 $ days ++;
4647}
4748
4849
4950/*
5051 * a single, complete string; may include years, months, days, hours, minutes,
51- * and "ago" if there's only one paramater and it's in the past
52+ * and "ago" if there's only one parameter and it's in the past
5253 */
5354
54- $ complete = array () ;
55+ $ complete = [] ;
5556
56- if ($ years )
57- {
58- $ complete [] = pluralize ('year ' , $ years );
59- }
60-
61- if ($ months )
62- {
63- $ complete [] = pluralize ('month ' , $ months );
64- }
65-
66- if ($ days )
67- {
68- $ complete [] = pluralize ('day ' , $ days );
69- }
70-
71- if ($ hours )
72- {
73- $ complete [] = pluralize ('hour ' , $ hours );
74- }
57+ if ($ years ) { $ complete [] = pluralize ('year ' , $ years ); }
58+ if ($ months ) { $ complete [] = pluralize ('month ' , $ months ); }
59+ if ($ days ) { $ complete [] = pluralize ('day ' , $ days ); }
60+ if ($ hours ) { $ complete [] = pluralize ('hour ' , $ hours ); }
61+ if ($ minutes ) { $ complete [] = pluralize ('minute ' , $ minutes ); }
7562
76- if ($ minutes )
77- {
78- $ complete [] = pluralize ('minute ' , $ minutes );
79- }
80-
81- if (sizeof ($ complete ) > 1 )
82- {
63+ if (count ($ complete ) > 1 ) {
8364 $ complete_string = implode (', ' , array_slice ($ complete , 0 , -1 ));
84- $ complete_string .= " and " .$ complete [sizeof ($ complete )-1 ];
85- }
86- else
87- {
65+ $ complete_string .= " and " .$ complete [count ($ complete )-1 ];
66+ } else {
8867 $ complete_string = implode (', ' , $ complete );
8968}
9069
91- if ( ! isset ($ dates [1 ]) AND $ sign === "+ " )
92- {
70+ if ( ! isset ($ dates [1 ]) && $ sign === "+ " ) {
9371 $ complete_string .= " ago " ;
9472}
9573
96- $ wf ->result (
97- "complete " ,
98- $ complete_string ,
99- $ complete_string ,
100- "copy to clipboard " ,
101- $ icon
102- );
74+ $ workflow ->item ()
75+ ->title ($ complete_string )
76+ ->arg ($ complete_string )
77+ ->subtitle ('Copy to clipboard ' );
10378
10479
10580/*
10681 * include business weeks if we have them
10782 */
10883
109- if ($ business_weeks > 0 )
110- {
111- $ wf ->result (
112- "business weeks " ,
113- pluralize ('business week ' , $ business_weeks ),
114- pluralize ('business week ' , $ business_weeks ),
115- "copy to clipboard " ,
116- $ icon
117- );
84+ if ($ business_weeks > 0 ) {
85+ $ workflow ->item ()
86+ ->title (pluralize ('business week ' , $ business_weeks ))
87+ ->arg (pluralize ('business week ' , $ business_weeks ))
88+ ->subtitle ('Copy to clipboard ' );
11889}
11990
12091
12192/*
12293 * include weeks if we have them
12394 */
12495
125- if ($ weeks > 0 )
126- {
127- $ wf ->result (
128- "weeks " ,
129- pluralize ('week ' , $ weeks ),
130- pluralize ('week ' , $ weeks ),
131- "copy to clipboard " ,
132- $ icon
133- );
96+ if ($ weeks > 0 ) {
97+ $ workflow ->item ()
98+ ->title (pluralize ('week ' , $ weeks ))
99+ ->arg (pluralize ('week ' , $ weeks ))
100+ ->subtitle ('Copy to clipboard ' );
134101}
135102
136103
139106 * we should include it because it's probably interesting
140107 */
141108
142- if ($ total_days > $ days )
143- {
144- $ wf ->result (
145- "days " ,
146- pluralize ('day ' , $ total_days ),
147- pluralize ('day ' , $ total_days ),
148- "copy to clipboard " ,
149- $ icon
150- );
109+ if ($ total_days > $ days ) {
110+ $ workflow ->item ()
111+ ->title (pluralize ('day ' , $ total_days ))
112+ ->arg (pluralize ('day ' , $ total_days ))
113+ ->subtitle ('Copy to clipboard ' );
151114}
152115
153116
154117/*
155118 * include a total count of hours if we've got them
156119 */
157120
158- if ($ total_hours > 0 )
159- {
160- $ wf ->result (
161- "hours " ,
162- pluralize ('hour ' , $ total_hours ),
163- pluralize ('hour ' , $ total_hours ),
164- "copy to clipboard " ,
165- $ icon
166- );
121+ if ($ total_hours > 0 ) {
122+ $ workflow ->item ()
123+ ->title (pluralize ('hour ' , $ total_hours ))
124+ ->arg (pluralize ('hour ' , $ total_hours ))
125+ ->subtitle ('Copy to clipboard ' );
167126}
168127
169128
170129/*
171130 * include a total count of minutes if we've got them
172131 */
173132
174- if ($ total_minutes > 0 )
175- {
176- $ wf ->result (
177- "minutes " ,
178- pluralize ('minute ' , $ total_minutes ),
179- pluralize ('minute ' , $ total_minutes ),
180- "copy to clipboard " ,
181- $ icon
182- );
133+ if ($ total_minutes > 0 ) {
134+ $ workflow ->item ()
135+ ->title (pluralize ('minute ' , $ total_minutes ))
136+ ->arg (pluralize ('minute ' , $ total_minutes ))
137+ ->subtitle ('Copy to clipboard ' );
183138}
184139
185-
186- echo $ wf ->toxml ();
140+ $ workflow ->output ();
187141
188142
189143/*
190144 * make pretty numbers, and add "s"'s if needed
191145 */
192146
193- function pluralize ($ label , $ value )
147+ function pluralize ($ label , $ value ): string
194148{
195- return number_format ($ value ) . " " . $ label . ($ value != 1 ? "s " : "" );
149+ return number_format ($ value ) . " " . $ label . ($ value !== 1 ? "s " : "" );
196150}
197-
198- ?>
0 commit comments