4

Is there any functionality in Emacs Lisp that behaves similar to PHP's strtotime function? (Actually AFAIK it implements relative items of the GNU date input formats.)

In PHP I can write

echo strtotime("+2 months"); //1258891352
echo strtotime("-3 months +2 weeks"); //1246952239

which return the corresponding UNIX timestamps.

skaffman
  • 398,947
  • 96
  • 818
  • 769
viam0Zah
  • 25,949
  • 8
  • 77
  • 100

3 Answers3

4

While not exactly what you're asking for, this may be of use. There's a command 'org-schedule in the package org-mode that has a really nice interface to choosing dates. The specific command that does it is 'org-read-date, which understands many ways of representing the date, including:

 +0            --> today
 .             --> today
 +4d           --> four days from today
 +4            --> same as above
 +2w           --> two weeks from today
 ++5           --> five days from default date
 +2tue         --> second Tuesday from now.

If you're looking for an interactive way to specify dates with that handy syntactic sugar, that routine should fit the bill nicely. If you're looking for a programmatic solution, it looks as though the above command calls 'org-read-date-analyze to do the leg work. Note: its usage is a little obscure (two of its arguments are never defined...), so if you can't figure it out directly, it might be worth sending mail to the org mailing list.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • A checked org-mode's date facilities but didn't find this function. Thanks. – viam0Zah Sep 22 '09 at 19:06
  • The function `org-read-date-analyze isn't a command, but is defined in org.el around line 13155 (for latest org.el: http://repo.or.cz/w/org-mode.git/blob/HEAD:/lisp/org.el) – Trey Jackson Dec 04 '09 at 19:51
  • I accept this answer. It does not cover exactly the same functionality what `strtotime` provides, but its goal is similar and is a native solution. – viam0Zah Mar 29 '10 at 16:07
3
(defun string-to-time (date)
  (shell-command-to-string (format "date --date='%s' +%%s" date)))
huaiyuan
  • 26,129
  • 5
  • 57
  • 63
1

The closest inbuilt function I know of is encode-time. If you want to use natural language, you'll probably have to end up writing something like this: http://github.com/chaitanyagupta/chronicity

artagnon
  • 3,609
  • 3
  • 23
  • 26