I have a view of users which displays fields, one of which is User: Last login. I set the Date format to Time ago (with "ago" appended). By default, this returns values like:

  • 1 hour 5 minutes ago
  • 7 hours 23 minutes ago
  • 3 days 17 hours ago

I am sorting the view by User: Last login as well, and for this I have set the Granularity to Day. For a number of reasons, I would like to prevent users from seeing precisely how long ago another user logged in if that user logged in within the last 24 hours. So instead, I would like to return values like:

  • 1 hour 5 minutes ago-> Today
  • 7 hours 23 minutes ago-> Today
  • 3 days 17 hours ago-> 3 days ago

Is it possible to do something like this as a custom Date format in Views?

  • I've updated my answer to be more complete now. :)
    – Letharion
    Jul 8, 2012 at 17:39

2 Answers 2


In the "dull ax" approach to things, you could do a combination of things here.

  1. In the Views UI, under the Date Format, there is a If "Custom", see the PHP docs for date formats. If "Time ago", enter the number of different time units to display, which defaults to 2. Set this to 1.

  2. Now, Views will only output 13 sec ago 13 min ago 1 hour ago etc instead of 1 day 13 hours etc.

  3. This still doesn't get rid of your specificity of the time of the day if today, eg it is going to output 1 sec ago, 12 min ago or 3 hour ago, etc.

  4. To take care of that, you can create a views template for that field, eg, here in D6 World it would be something like views-view-field--NAMEOFVIEW--login.tpl.php.

  5. In there, do something along the lines of:

if (strpos($output, 'sec') !== FALSE || strpos($output, 'min') !== FALSE || strpos($output, 'hour') !== FALSE) {
  print t('Today');
else {
  print $output;

which will print out Today if it sees a sec, min or hour, or the default if not, eg, it sees day, week, month etc.

  • The fifth step will not work with multilingual sites, but you can improve it.
    – kalabro
    Jul 6, 2012 at 8:17
  • true, t('sec'), t('min'), t('hour') would sharpen the ax a bit.
    – Jimajamma
    Jul 6, 2012 at 15:47
  • I tried this: if (strpos($output, t('sec')) !== FALSE || strpos($output, t('min')) !== FALSE || strpos($output, t('hour')) !== FALSE) { This works for English, but it doesn't work for Japanese. Any ideas? Jul 8, 2012 at 15:44
  • check and make sure you have translations for the strings sec, min and hour in Japanese. I've never done any, but these appear to be a good start on how: drupal.org/contribute/translations and drupal.org/project/l10n_update
    – Jimajamma
    Jul 8, 2012 at 18:16
  • if that's not the case, as I am really naive when it comes to internationalization, perhaps mb_strpos() or one of its multibyte cousins is the way to go.
    – Jimajamma
    Jul 8, 2012 at 19:41

I think the logic required for of "Today" vs "3 days ago" rules out using a regular Date format. If you stuck to "X days ago", it would work, but that's not as pretty.

I would implement a custom field formatter that did what you wanted, as it's an easy way to get high control over output.

There is a drawback in this case, since you don't actually have a proper field for "Last login", you need to add an extra "dummy" field. It's up to you if this is an acceptable work-around, but I've used this several times myself to no I'll effect. If you put it on a end-user editable entity, you will probably want to deny access to the field for users though, so it doesn't clutter the UI.

To keep topics separate, I posted that as a separate question. How do I make the output of field X look like Y?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.