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

Is there any function/code snippet to retrieve the unix time stamp of the comment on a post? The WordPress default function comment_time(); returns the time of the post in 12hr format (not helpful).

share|improve this question
up vote 1 down vote accepted

Use:

global $comment;
$timestamp = strtotime("{$comment->comment_date_gmt} GMT");

Regards.

share|improve this answer
1  
That works! Thank you for the inputs. – John Nov 2 '11 at 17:39
1  
Don't thank me. Vote me ;) – EarnestoDev Nov 2 '11 at 17:55

I know you've already selected an answer, but just a note based on your comments: there's a human_time_diff() function in WordPress. http://codex.wordpress.org/Function_Reference/human_time_diff

share|improve this answer

This deserves a new answer, because the answer is simple.

WordPress is written in the programming language PHP. The date formatting functions in WordPress use PHP's built-in date formatting functions.

This quote can be found on the wordpress codex here

This information is super important because PHP has the following formatting tag 'U' The complete list can be found here.

In other words. The answer to this question is: comment_time('U');

Perhaps this wasn't the case in 2011, but it is in 2016.

share|improve this answer
    
just because an answer is accepted doesn't mean it is the best possible one, I don't think anything changed since then ;) – Mark Kaplun Feb 20 at 19:31
    
@MarkKaplun Oh, I thought maybe wordpress was originally written in Perl or something :3 I overlooked that tidbit on the codex page probably four or five times until I noticed it and looked up the PHP date formats. – Josh C Feb 21 at 0:02

Not sure about how or why you need the unix time stamp.

You can get the ISO 8601 time stamp by using

<?php the_time('c'); ?>

This will output something like this

2011-01-22T12:01:09+00:00

It may not be the unix timestamp, but at least you can use that to convert to a unix timestamp with php if you need to.

Here is a PHP function to convert to UNIX timestamp. Taken from google calendar for simplepie plugin.

    function tstamptotime($tstamp) {
            // converts ISODATE to unix date
            // 1984-09-01T14:21:31Z
            sscanf($tstamp,"%u-%u-%uT%u:%u:%uZ",$year,$month,$day,$hour,$min,$sec);
            $newtstamp=mktime($hour,$min,$sec,$month,$day,$year);
            return $newtstamp;
    }

Update

If you want to get the relative time for comments, there is an easier way than trying to work out the time stamp in unix and doing all these conversions.

Here are 2 functions I am using in the theme framework I am building that will do the job for you.

First add this to the theme functions.

function theme_time_passed ($t1, $t2)
{
    if($t1 > $t2) :
      $time1 = $t2;
      $time2 = $t1;
    else :
      $time1 = $t1;
      $time2 = $t2;
    endif;
    $diff = array(
      'years' => 0,
      'months' => 0,
      'weeks' => 0,
      'days' => 0,
      'hours' => 0,
      'minutes' => 0,
      'seconds' =>0
    );
    $units = array('years','months','weeks','days','hours','minutes','seconds');
    foreach($units as $unit) :
      while(true) :
         $next = strtotime("+1 $unit", $time1);
         if($next < $time2) :
            $time1 = $next;
            $diff[$unit]++;
         else :
            break;
         endif;
      endwhile;
    endforeach;
    return($diff);
}

function theme_time_since($thetime) 
{
    $diff = theme_time_passed($thetime, strtotime('now'));
    $units = 0;
    $time_since = array();
    foreach($diff as $unit => $value) :
       if($value != 0 && $units < 2) :
            if($value === 1) :
                $unit = substr($unit, 0, -1); #removes the plural "s"
            endif;
           $time_since[]= $value . ' ' .$unit;
           ++$units;        
        endif;
    endforeach;
    $time_since = implode(', ',$time_since);
    $time_since .= ' ago';
    $date = $time_since;
    return $date;

}

Now you can get a relative time for any date format in worpdress.. either posts or comments.

For comments you can do something like this in the comments code.

<?php echo theme_time_since(get_comment_time('U')) ?>

Or for post date, use this inside the loop.

<?php echo theme_time_since(get_the_time('U')) ?>

From what I understand you want to do, this will work. It outputs the date and time as number of days, hours etc relative to today.

eg:

45 seconds ago, or 1 day 1hr ago.. etc

share|improve this answer
    
Thanks for your inputs. The reason I need the unix time stamp is because I have a function that takes the unix stamp and converts it into relative time (like the ones you see on social media sites). Your suggestion does indeed work. However, I guess you missed the point that I need to retrieve the time of the "post comment" and not the post itself, which is what <?php the_time('c'); ?> function does. Any suggestions? – John Nov 2 '11 at 13:46
    
When you say "converts it into relative time (like the ones you see on social media sites)." Do you mean like "posted 1 day and 13 hours ago" ? – Andy James Nov 2 '11 at 13:59
    
Yes, sorry, I did miss that is was for comments. But the same principal applies.. using 'c' as the parameter. comment_date('c'); Will give you the date format as ISO – Andy James Nov 2 '11 at 14:17
    
@John See the update to my original answer. It will get you the relative time how "I think" you want it. – Andy James Nov 2 '11 at 14:29
    
Your function for relative time solves the problem I had. However, I find there is a difference in time displayed in respect to the GMT time. Any ideas? Anyways a brilliant solution! – John Nov 2 '11 at 17:34

Your Answer

 
discard

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.