Adam Kiss Posted January 12, 2013 Posted January 12, 2013 TimeFormat for ProcessWire v1.0.3 This is a tiny ProcessWire Modules used for formatting times as 'X seconds ago', 'minutes ago'. It goes up to decades. Important note: this module uses 'soft limit' for units. That means that it will show '8 days ago' for 8 days, but '1 week ago' for 9 days, for instance. This will be configurable in one of the later releases. Usage It's very simple. Load module, pass the timestamp to `format` function. Bam, done. <?php $time_format = $modules->get('ToolsTimeFormat'); $posted = $time_format->ago($page->created); Links GitHub modules.processwire.com Notes v1.0.2 & v1.0.3 - bugfixing and module rename 4
Pete Posted January 12, 2013 Posted January 12, 2013 I like this as I spend a lot of time on forums and these little things make it seem more friendly. Nice work!
Pete Posted January 12, 2013 Posted January 12, 2013 Something that the forum software does is that it only limits it to seconds, minutes and hours ago, then in other places says "Today at X:XXam" and finally "Yesterday at X:XXam" so it would be nice to have those options and set a cut-off in a future version so that "if the date is before yesterday then just return the date formatted with date()" for example The thing is, once you get past today and yesterday, you have to think about your use case and whether "11 months ago" is actually that descriptive. Not a criticism, just a note that when using this module you have to think about where you're going to use this and how accurate you want to be.
Adam Kiss Posted January 12, 2013 Author Posted January 12, 2013 Something that the forum software does is that it only limits it to seconds, minutes and hours ago, then in other places says "Today at X:XXam" and finally "Yesterday at X:XXam" so it would be nice to have those options and set a cut-off in a future version so that "if the date is before yesterday then just return the date formatted with date()" for example The thing is, once you get past today and yesterday, you have to think about your use case and whether "11 months ago" is actually that descriptive. Not a criticism, just a note that when using this module you have to think about where you're going to use this and how accurate you want to be. I hear ya. 11 months ago has place for blogs for one There are many things planned, namely: limiting the level you go into (maybe you want something like 432 days and don't want it translated into 14 months) other formats (today at XX:YY, for instance) configurability of soft limits simpler way to go to hard limits than defining precise soft limits I just wanted this one out quickly, because I also planned to introduce new Modules group with it which I hope will catch on. 1
Soma Posted January 12, 2013 Posted January 12, 2013 Can't install. 2013-01-12 23:03:28 admin pwprofile/processwire/module/ Parse Error syntax error, unexpected ')' (line 78 of /Applications/XAMPP/xamppfiles/htdocs/pwprofile/site/modules/ToolsTimeAgo.module)
Adam Kiss Posted January 12, 2013 Author Posted January 12, 2013 Fixed. I also bumped version, so the fix gets picked up by ModulesManager
Soma Posted January 12, 2013 Posted January 12, 2013 Translation doesn't work. Or am I missing something? ProcessLanguageTranslator: Found /site/modules/ToolsTimeAgo.module ProcessLanguageTranslator: That file has no translatable phrases Translation doesn't work for runtime variables as the string. And I thought the _() have to be on 1 line, but I could be wrong.
Adam Kiss Posted January 12, 2013 Author Posted January 12, 2013 Well, the strings should be prepared for translation, even though I haven't really played with translations before. I use static (read: always the same) strings for translation, although there might be problem with them being passed through variables. Unfortunately, there can't be $this->() call in class variable definition. Would it help if I moved the those string definitions into init() function? Any help here would be great! Also, Soma, thanks for spotting all the errors I am personally happy with my fast shipping (for a change!), but these things happen, and another set of eyes is always welcome. So, thanks again!
Soma Posted January 12, 2013 Posted January 12, 2013 Fixed. I also bumped version, so the fix gets picked up by ModulesManager Hmm i don't see it in modules manager.
Adam Kiss Posted January 12, 2013 Author Posted January 12, 2013 Yeah well, when I wrote that, I forgot for a while that it has to be confirmed on modules.processwire.com by Ryan, I suppose. So many mistakes… But, I repeat, it's out! 1
Adam Kiss Posted January 13, 2013 Author Posted January 13, 2013 This module has been renamed to ToolsTimeFormat to accomodate future funcionality better. Sorry for any inconvenience.
Soma Posted January 13, 2013 Posted January 13, 2013 Can you rename the title also to ToolsTimeFormat, because after installing one could search the "Time" group when it actually is in the "Tools".
Soma Posted January 13, 2013 Posted January 13, 2013 Just asking myself, how is this modules working together with cache? Wouldn't be possible I guess.
Martijn Geerts Posted January 13, 2013 Posted January 13, 2013 $then = new DateTime((is_int($timestamp) ? "@{$timestamp}" : $timestamp)); Learn me PHP, whats is that @ sign doing there ?
Adam Kiss Posted January 13, 2013 Author Posted January 13, 2013 DateTime() takes a string argument when creating new object - it takes the very same arguments as strtotime() does. It's a natural date parser, so if you want to tell it that 'this number is a timestap', you have to prepend '@' before it. That means, that you could theoretically do this: $tf = $modules->get('ToolsTimeFormat'); echo $tf->ago('2012/12/24 18:00'); Edit: +bonus tiny english lesson 'learn' is a word you use when you gain information ('I'm learning'). When you give information, the verb is 'teaching' (e.g. "I am teaching you, you are learning from me"). Hope that helps. (as an absolute off-topic, if I ever make a mistake, you are absolutely free to correct me, so I can learn something new ) 2
diogo Posted January 13, 2013 Posted January 13, 2013 Here is something for us all to learn. Martijn's mistake happened because the dutch word "leren" means learn and teach at the same time. Strange fact but true, courtesy of google translate I suspected it could be something like this, because the word "lernen" in German means both learn and study, and I always found it funny when German people would say to me in English "leave me alone, i have to learn now".
Martijn Geerts Posted January 13, 2013 Posted January 13, 2013 Never to old for learning. It's exactly as diogo suggests. Thanks adamkiss & diogo. Will remember your module adamkiss, tnx for your work. I will use it after I translated it to dutch.
gornycreative Posted April 19, 2020 Posted April 19, 2020 Anyone curious, this module needs a namespace for v3. The module file should begin: <?php namespace Processwire; use DateTime;
Robin S Posted April 19, 2020 Posted April 19, 2020 FYI, the core has built-in support for relative time strings: https://processwire.com/api/ref/wire-date-time/relative-time-str/ 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now