Skip to content

Commit 55bfe6f

Browse files
committed
First commit.
0 parents  commit 55bfe6f

File tree

6 files changed

+790
-0
lines changed

6 files changed

+790
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Use this workflow to get the difference between two dates in Alfred. This relies exclusively on PHP 5.3+'s [DateInterval](http://www.php.net/manual/en/dateinterval.format.php) method, which is happy to accept a variety of inputs and works pretty well! Examples:
2+
3+
- `datespan tomorrow`: countdown in hours and minutes
4+
- `datespan 10/10/10`: difference in years, months, days, hours, and minutes—along with total business weeks, weeks, days, hours, and minutes
5+
- `datespan 3/10 to 5/12`: all units above, but the span between zero-hour on each date
6+
7+
You can use any format that [`strtotime`](https://php.net/manual/en/function.strtotime.php) can parse, so I'm sure there are plenty of variations I've not thought to try yet.
8+
9+
Screenshots with a few more examples [on my humble blog](http://workingconcept.com/blog/date-span-alfred-workflow).
10+
11+
Comments, suggestions, criticisms, and pull requests all welcome!

datespan.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
$timezone_abbreviation = isset($argv[2]) ? $argv[2] : 'PDT';
4+
5+
date_default_timezone_set(timezone_name_from_abbr($timezone_abbreviation));
6+
7+
$icon = "icon.icns";
8+
9+
require( 'workflows.php' ); // by David Ferguson
10+
$wf = new Workflows();
11+
12+
$dates = explode(" to ", trim(@$argv[1])); // split arguments by " to "
13+
14+
for ($i=0; $i < sizeof($dates); $i++)
15+
{
16+
$dates[$i] = str_replace('-', '/', $dates[$i]);
17+
}
18+
19+
20+
/*
21+
* use the current date and time in lieu of a second argument
22+
*/
23+
24+
$date2 = isset($dates[1]) ? new DateTime($dates[1]) : new DateTime();
25+
$date1 = new DateTime($dates[0]);
26+
27+
$diff = $date1->diff($date2);
28+
29+
// http://www.php.net/manual/en/dateinterval.format.php
30+
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'));
41+
$sign = $diff->format('%R');
42+
43+
if ($total_days > 1)
44+
{
45+
$days++;
46+
}
47+
48+
49+
/*
50+
* 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+
*/
53+
54+
$complete = array();
55+
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+
}
75+
76+
if ($minutes)
77+
{
78+
$complete[] = pluralize('minute', $minutes);
79+
}
80+
81+
if (sizeof($complete) > 1)
82+
{
83+
$complete_string = implode(', ', array_slice($complete, 0, -1));
84+
$complete_string .= " and ".$complete[sizeof($complete)-1];
85+
}
86+
else
87+
{
88+
$complete_string = implode(', ', $complete);
89+
}
90+
91+
if ( ! isset($dates[1]) AND $sign === "+")
92+
{
93+
$complete_string .= " ago";
94+
}
95+
96+
$wf->result(
97+
"complete",
98+
$complete_string,
99+
$complete_string,
100+
"copy to clipboard",
101+
$icon
102+
);
103+
104+
105+
/*
106+
* include business weeks if we have them
107+
*/
108+
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+
);
118+
}
119+
120+
121+
/*
122+
* include weeks if we have them
123+
*/
124+
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+
);
134+
}
135+
136+
137+
/*
138+
* if the *total* number of days is different from days factoring into the interval,
139+
* we should include it because it's probably interesting
140+
*/
141+
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+
);
151+
}
152+
153+
154+
/*
155+
* include a total count of hours if we've got them
156+
*/
157+
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+
);
167+
}
168+
169+
170+
/*
171+
* include a total count of minutes if we've got them
172+
*/
173+
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+
);
183+
}
184+
185+
186+
echo $wf->toxml();
187+
188+
189+
/*
190+
* make pretty numbers, and add "s"'s if needed
191+
*/
192+
193+
function pluralize($label, $value)
194+
{
195+
return number_format($value) . " ". $label . ($value != 1 ? "s" : "");
196+
}
197+
198+
?>

icon.icns

30.6 KB
Binary file not shown.

icon.png

9.54 KB
Loading

info.plist

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>bundleid</key>
6+
<string>workingconcept.datespan</string>
7+
<key>category</key>
8+
<string>Tools</string>
9+
<key>connections</key>
10+
<dict>
11+
<key>417EDFE4-EEA3-4586-BACB-72B5CD1488FA</key>
12+
<array>
13+
<dict>
14+
<key>destinationuid</key>
15+
<string>D06D3D35-232B-4572-8123-A7BE7F9A5CE7</string>
16+
<key>modifiers</key>
17+
<integer>0</integer>
18+
<key>modifiersubtext</key>
19+
<string></string>
20+
</dict>
21+
</array>
22+
</dict>
23+
<key>createdby</key>
24+
<string>Matt Stein</string>
25+
<key>description</key>
26+
<string>Calculate the difference between dates.</string>
27+
<key>disabled</key>
28+
<false/>
29+
<key>name</key>
30+
<string>Date Span</string>
31+
<key>objects</key>
32+
<array>
33+
<dict>
34+
<key>config</key>
35+
<dict>
36+
<key>autopaste</key>
37+
<false/>
38+
<key>clipboardtext</key>
39+
<string>{query}</string>
40+
</dict>
41+
<key>type</key>
42+
<string>alfred.workflow.output.clipboard</string>
43+
<key>uid</key>
44+
<string>D06D3D35-232B-4572-8123-A7BE7F9A5CE7</string>
45+
<key>version</key>
46+
<integer>0</integer>
47+
</dict>
48+
<dict>
49+
<key>config</key>
50+
<dict>
51+
<key>argumenttype</key>
52+
<integer>0</integer>
53+
<key>escaping</key>
54+
<integer>36</integer>
55+
<key>keyword</key>
56+
<string>datespan</string>
57+
<key>script</key>
58+
<string>TIMEZONE=`date +%Z`
59+
php -f datespan.php -- "{query}" $TIMEZONE</string>
60+
<key>title</key>
61+
<string>Date Span</string>
62+
<key>type</key>
63+
<integer>0</integer>
64+
<key>withspace</key>
65+
<true/>
66+
</dict>
67+
<key>type</key>
68+
<string>alfred.workflow.input.scriptfilter</string>
69+
<key>uid</key>
70+
<string>417EDFE4-EEA3-4586-BACB-72B5CD1488FA</string>
71+
<key>version</key>
72+
<integer>0</integer>
73+
</dict>
74+
</array>
75+
<key>readme</key>
76+
<string>Add a single date to see the difference in years, months, business weeks, weeks, days, hours, and minutes from today.
77+
78+
Add two dates to see the difference between the two, in the same units as above.
79+
80+
This will use any string that PHP can convert into a date, so all the following are valid input…
81+
82+
- “1/1/13”
83+
- “1/1/13 to 10/31/13”
84+
- “january 1”
85+
- “january 1 to march 31”
86+
- “8:15pm”
87+
- “9am to 5pm”
88+
89+
Please improve this plugin if you’d like, or just say hi—I’d love to know if you’ve found it useful!
90+
91+
- Matt ([email protected])</string>
92+
<key>uidata</key>
93+
<dict>
94+
<key>417EDFE4-EEA3-4586-BACB-72B5CD1488FA</key>
95+
<dict>
96+
<key>ypos</key>
97+
<real>60</real>
98+
</dict>
99+
<key>D06D3D35-232B-4572-8123-A7BE7F9A5CE7</key>
100+
<dict>
101+
<key>ypos</key>
102+
<real>10</real>
103+
</dict>
104+
</dict>
105+
<key>webaddress</key>
106+
<string>http://workingconcept.com</string>
107+
</dict>
108+
</plist>

0 commit comments

Comments
 (0)