Unix timestamp for post comment

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).

Related posts

Leave a Reply

4 comments

  1. 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.

  2. 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