---
title: Natural language dates for TextExpander
date: 2011-10-15 06:06
slug: natural-language-dates-for-textexpander
categories:
- Code
tags:
- textexpander
- snippet
- php
- naturallanguage
---

[date math snippets]: https://gist.github.com/1282431
[Donald Curtis]: http://milkbox.net/
[strtotime]: http://us2.php.net/manual/en/function.strtotime.php
[tools]: http://brettterpstra.com/project/textexpander-tools/#tools-emem
[download]: http://brettterpstra.com/share/te-snippets/index.php?group=Tools&prefix=,,
[te project]: http://brettterpstra.com/project/textexpander-tools/
[natlangservice]: http://brettterpstra.com/os-x-service-for-natural-language-dates/

I had a strong urge to make a new [TextExpander](http://smilesoftware.com/TextExpander/) snippet this morning. I think the Date Math features of TextExpander that [Donald Curtis][] turned me on to with his [Date Time group][date math snippets] got me thinking.

I find my [OS X Service for natural language date conversion][natlangservice] pretty handy. I think TextExpander could make it handier, though, with the form fill feature and a little more flexibility.

I've added the new snippet to the [Tools group][tools] in the [TextExpander Project][te project], so TextExpander users can [grab the download or self-updating URL][download] and play with it.

### Of note

It accepts most basic date language. "today", "tomorrow", "3 days", "2 days ago", "next tue", "last fri", etc.

I added a couple of checks. If you use a plus (+) or minus (-) sign followed immediately by a number and that's all, it will just add or subtract that many days from the current date. The plus sign is optional, it will work with just "3" to give you the date 3 days from now.

If the words "am", "pm", "noon", "midnight", "hours" or "minutes" are in your string, it will add the time string to the formatted output.

<!--more-->

### Setup

This one runs on PHP, so it's ready to use with your built-in tools and has no additional dependencies. There are a few things you can edit (and probably should) once you have it open in TextExpander. 

First, [set the timezone](http://php.net/manual/en/timezones.php) correctly for your location in the third line. The next line has the primary date format, which defaults to "l, M jS, Y", like this: Saturday, Oct 15th, 2011. The line after that is the string that's appended if you use a phrase that triggers a time for the output, defaulting to " \a\\t g:ia" which comes out as "at 3:00pm".

The $debug parameter can be set to 0, 1 or 2. At zero, if your string fails to translate it will fail silently. At 1, it will return the phrase "Derp" where you meant to paste the date. I figure some feedback is better than none... at 3 it will give you the "Derp" *and* a Growl notification if you have `growlnotify` installed.

### The code

You can just install this from the [project page][download], but here's the code if your curious. This looks a little different than what you see in the snippet, I'll show you why in a second.

```php
#!/usr/bin/env php
<?php
date_default_timezone_set("America/Chicago");
$dateformat = "l, M jS, Y";
$timeformat = " \a\\t g:ia";
$debug = 2;

$input="%fill:Date string%";

if ( preg_match( "/^([+-])?(\d+)$/", trim($input), $matches ) ) 
{
  $input = $matches[2] . " days";
  if ($matches[1] == "-") $input .= " ago";
} 
else if ( preg_match("/([ap]m|noon|midnight|hours|minutes)/i",trim($input),$matches) ) 
{
  $dateformat .= $timeformat;
}

if (($timestamp = strtotime("$input")) === false) 
{
  if($debug > 1) 
    `/usr/local/bin/growlnotify -a TextExpander -t "Natural language FAIL" -m "I can't understand you when you mumble"`;
  if ($debug > 0) 
	die('Derp');
} 
else 
{
  echo date($dateformat,$timestamp);
}
```

Because we're using the newly-shell-script-friendly Fill feature, normally the entire script would show up in the fill box. That's ugly. If we add some spacing before and after the Fill placeholder, we can clear most of the excess out. Like so:

<pre><code>
[...]
$input=

				"%fill:Date string%";
				// mon, tue, next fri, tomorrow 3pm...
				// 1, 3, -2 (days), 30 minutes...
				// avoid "on" and "at", keep it simple.

if ( preg_match( "/^([+-])?(\d+)$/", trim($input), $matches ) )
[...]
</code></pre>

Looks like this now:

![Fill dialog cleaned up](/uploads/2011/10/fill_dialog_cleaned_up.jpg)

Hopefully this will be useful for some of you!

---

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff. This content is supported by readers like you. [More about Brett](https://brettterpstra.com/contact.md), including social links.

Copyright © 2026 Brett Terpstra. All rights reserved.
