Parsing Natural Language Date Strings in PHP

Published Friday July 3, 2020

I'm constantly surprised and frustrated at how difficult it is to get and use date and time input from users. I remember being frustrated by it in the 90s and it doesn't feel like we've come all that far since then. To have a truly consistent interface for users we often reach for complex javascript-powered date pickers. Even the native browser implementations of HTML5 date and time fields make users want to reach for the mouse, creating a bottleneck in every form that uses them.

My family uses Cozi Family Organizer to do our meal planning and make sure that at least one adult is scheduled to be in the same state as the children every day (less of a problem in the age of covid). Their web app has a really nice open text add to calendar feature that very accurately parses sentences into calendar events.

Screen Shot 2020-07-03 at 2 48 22 PM

I use this feature dozens of times a week and every time I do I wish I could give my users the same experience. This week I reached my pain threshold and decided to do something about it. I definitely have not duplicated what Cozi's interface offers, but I do have a pretty good jumping off point for letting people enter date and time in a natural, keyboard-only manner.

Introducing Sundial

On Thursday I built Sundial, a natural language date and time parser that does a pretty good job of pulling dates and times out of blocks of text. It currently works with absolute dates and times (June 11th at 4:15 pm), with relative dates and times (next thursday at noon) limited to strings that strtotime() can parse on its own. This minimum viable package meets the core need I have at work. But I'm hoping that with a little help from the community we can take it WAY furhter.

Try To Break Sundial (Please)

Contribute on GitHub

Did you find a mistake? Have something to add? This article is on GitHub. Send me a PR!

Random

© 2020 All rights reserved.

;