Flexible date manipulation in javascript

Date manipulation is a common programming task a developer does regularly and many times can get tricky and cumbersome. If youy web applications relies heavily on date manipulation on the frontend than moment.js is the library you have been looking for.

In short this is what you can do with moment.js

moment("20111031", "YYYYMMDD").fromNow(); // 2 years ago
moment().endOf('day').fromNow(); // in 2 hours
moment().startOf('hour').fromNow(); // 9 minutes ago
 
moment().subtract('days', 6).calendar(); // Last Wednesday at 10:09 PM
moment().add('days', 1).calendar(); // Tomorrow at 10:10 PM

(more…)

Read More

Printing relative dates in php

A couple of days back while writing some date code for a messaging service, I required to print the date of the messages in a relative format – ‘today, ‘yesterday’, 3 weeks ago’ etc. I wrote a small function for the same. A sample run of the function is shown below.

 
echo DateToWords(time()) . "<br />";
echo DateToWords(time() - (3600 * 24 * 1)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 4)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 7)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 14)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 100)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 366));

And the output for the above. For dates above 1 year it returns the actual date.

 
today
yesterday
4 days ago
1 week ago
2 weeks ago
14 weeks ago
06-17-2009

(more…)

Read More