开发者

translating text like "next tuesday 5pm" to dates (similar to google calendar)

开发者 https://www.devze.com 2023-02-11 13:35 出处:网络
This wouldnt be too hard to code myself, but开发者_开发知识库 I\'m thinking to myself -- theres got to be some javascript or php library like this already out there...

This wouldnt be too hard to code myself, but开发者_开发知识库 I'm thinking to myself -- theres got to be some javascript or php library like this already out there...

Basically I want to translate normal speach-like text, such as "next monday", "tomorrow", "Jan 2", "2/15" at 4pm, etc.. into datetimes, similar to how google calendar input is very flexible with what you can input.

Anyone know of any particular libraries like this?


Php has the strtotime function.

Examples from the documentation:

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>


in php strtotime can do this, maybe not as good as google though:

<?php
echo strtotime("now"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>


For a JavaScript solution, try http://www.datejs.com/.

It seems to be similar to what you're looking for. It actually didn't work with "next tuesday 5pm" but did work for "tuesday 5pm." It may be possible to add to it to take into possible cases that you think you may need but it doesn't support (like the example you gave).


It's easy with strtotime();

Just type strtotime("Next Tuesday");

http://php.net/manual/en/function.strtotime.php

0

精彩评论

暂无评论...
验证码 换一张
取 消