WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this script to get the last time a user logged in

function get_last_login($user_id) {
    $last_login = get_user_meta($user_id, 'last_login', true);
    echo human_time_diff($last_login) . " " . __('ago');

I am calling it in author.php with

<p>Last login: <?php get_last_login($userdata->ID); ?></p>

I am trying to output like "last login X days ago" but I can't get it working.

$last_login output is

2011-05-13 18:00:06

but the final output I get is

last login 15108 days ago

share|improve this question
up vote 3 down vote accepted

Are you formatting the mysql2date() input string as 'Y-m-d H:i:s', as specified in the Codex?

Also, why not use this same format as $date_format?


  1. What output do you get for $last_login?
  2. The second argument in human_time_diff() is optional. Why not just omit it? That way, if you get valid output from $last_login, you should get valid output from human_time_diff().


The human_time_diff() function expects a UNIX timestamp for its first argument. Try wrapping $last_login in mktime(), e.g.:

$last_login_unix = mktime( $last_login );

human_time_diff( $last_login_unix );


Might want to use strtotime() instead of mktime():

$last_login_unix = strtotime( $last_login );

human_time_diff( $last_login_unix );
share|improve this answer
You're right Chip. I pasted the code from an example I found. Now I'm trying to output with this code: function get_last_login($user_id) { $last_login = get_user_meta($user_id, 'last_login', true); echo human_time_diff($last_login, current_time('timestamp')) . " " . __('ago'); but is not outputting anything. – Andycap May 13 '11 at 19:27
Can you edit your question, to add/update your code? (See answer edits, above.) – Chip Bennett May 13 '11 at 19:37
Edited the question. – Andycap May 13 '11 at 19:49
See edited answer. :) – Chip Bennett May 13 '11 at 19:59
(And one more edit.) – Chip Bennett May 13 '11 at 20:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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