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 |
The function code is shown below.
<?php <?php /* Change the following constants to suit your language */ define('STRING_TODAY', "today"); define('STRING_YESTERDAY', "yesterday"); define('STRING_DAYS', "%d days ago"); define('STRING_WEEK', "1 week ago"); define('STRING_WEEKS', "%d weeks ago"); /* Change the following date format to your taste */ define('DATE_FORMAT', "m-d-Y"); /* The functions takes the date as a timestamp */ function DateToWords($time) { $_word = ""; /* Get the difference between the current time and the time given in days */ $days = intval((time() - $time) / 86400); /* If some forward time is given return error */ if($days < 0) { return -1; } switch($days) { case 0: $_word = STRING_TODAY; break; case 1: $_word = STRING_YESTERDAY; break; case ($days >= 2 && $days <= 6): $_word = sprintf(STRING_DAYS, $days); break; case ($days >= 7 && $days < 14): $_word= STRING_WEEK; break; case ($days >= 14 && $days <= 365): $_word = sprintf(STRING_WEEKS, intval($days / 7)); break; default : return date(DATE_FORMAT, $time); } return $_word; } ?> |
Hope someone finds this useful.
This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.
11 Responses
1
Jani Hartikainen
June 19th, 2010 at 1:36 pm
If I may make a suggestion, maybe sprintf should be used instead of concatenating strings. Basically, you would define STRING_DAYS, HOURS and WEEKS so, that you can use a sprintf placeholder. This way the relative position of the number with the text isn’t locked down, like it is now locked down to only allow text after the number.
2
Daniel
June 19th, 2010 at 4:01 pm
Might want to have a look at http://php.net/dateinterval.format as well.
sameer
June 19th, 2010 at 8:53 pm
The reason for not using DateInterval::format, is that it is only supported for PHP v 5.3.0 and greater.
4
Adam Maschek
June 22nd, 2010 at 1:49 pm
Agreed with Jani.
2 days ago in spanish = Hace 2 días, with concat you are already screwed.
5
Printing relative dates in php - Cyprus PHP Community - Cyprus PHP
June 22nd, 2010 at 2:25 pm
[...] Borate of codediesel.com took this time and did quite a wonderful job. A couple of days back while writing some date code [...]
sameer
June 22nd, 2010 at 7:54 pm
nice observation guys!
7
Les
June 23rd, 2010 at 9:02 am
Instead of polluting the global space with DEFINEs what you could do is use CONSTs instead from within the class – where they belong IMHO…
Makes the code more portable if for example, you move to another application? If you have a brain (and I feel that you do) then use it
sameer
June 23rd, 2010 at 8:48 pm
Yes they would belong in a class, but I’ve not defined a class here, it is a function. And it would be a bad idea to put the constants in the function. If someone wanted to change the string format he would have to dig into the function to change it. Better to use ‘define’ and save it into a language or config file. And anyways in such kind of issues you can argue from any direction, so it all depends on yours tastes.
- p.s The last time I checked I had a brain, but who knows, maybe I’m a zombie contributing something to the world, however small, instead of sitting on my butt and passing smartass remarks.
9
Caio Moritz Ronchi
June 26th, 2010 at 7:25 pm
That’s a clever implementation, simple and readable. I’ll probably be using that function for my own projects, possibly in a class/object oriented fashion. Thanks for sharing this.
10
gespadas.com » Calculando una Fecha Relativa con PHP
October 8th, 2010 at 5:50 pm
[...] código de FechaRelativa() está inspirado en la idea de los amigos de Code Diesel. MeneameBitacorasFacebookTwitter Categorías: Desarrollo Etiquetas: php blog comments [...]
11
Leonardo Aquino
September 24th, 2012 at 5:48 am
Excelent Post! Thank you so much!