Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Human time difference – won't work

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #190263
    stanmoor
    Participant

    Hi all

    New to PHP so I don’t know what I’m doing wrong here. I’m essentially trying to change the date format of each post so that it uses the human time difference function.

    I’ve figured that the date information (cb_date ?) is built into core.php. This is the line that i’ve found:

    $cb_date = ‘

    <i class=”fa fa-clock-o”></i> <time class=”updated” datetime=”‘ . get_the_time(‘Y-m-d’, $cb_post_id) . ‘”>’ . date_i18n( get_option(‘date_format’), strtotime(get_the_time(“Y-m-d”, $cb_post_id )) ) . ‘</time>

    ‘;

    I’ve tried adding in human_time_diff just before get_the_time and then opening brackets, and i’ve tried pasting in

    <?php echo human_time_diff( get_the_time(‘U’), current_time(‘timestamp’) ) . ‘ ago’; ?>

    which didn’t work. I get errors of ‘unexpected ;’ and if I delete the ; at the end of the line (which im sure is wrong), I get a T_STRING parse error.

    My website is http://www.thebookofbloke.com

    I would be grateful if anybody could assist.

    Thanks

    #190285
    stanmoor
    Participant

    @chrisburton OK so i’ve tried this:

    $cb_date = ‘<i class=”fa fa-clock-o”></i> <time class=”updated” datetime=”‘ . human_time_diff(get_the_time(‘Y-m-d’, $cb_post_id)) . ‘”>’ . date_i18n( get_option(‘date_format’), strtotime(human_time_diff(get_the_time(“Y-m-d”, $cb_post_id ))) ) . ‘</time>‘;

    Which resulted in ‘unexpected ;’, and i’ve tried this:

    $cb_date = ‘<i class=”fa fa-clock-o”></i> <time class=”updated” datetime=”‘ . human_time_diff(‘Y-m-d’, $cb_post_id) . ‘”>’ . date_i18n( get_option(‘date_format’), strtotime(human_time_diff(“Y-m-d”, $cb_post_id ))) . ‘</time>‘;

    Which also resulted in an error. Am i fundamentally on the right tracks here? I just want it to display X minutes / hours / days ago !

    Also, on a side note, a lot of tutorials say that I should be looking for <?php echo the_time() or something to that effect – but it is nowhere in my theme. What is the reason for this?

    Thanks for your help.

    #190288
    stanmoor
    Participant

    @chrisburton

    Hi Chris,

    The code you’ve copied above (from what I can see) is the same as the theme’s default but with an additional ‘;’ – I’m looking to have the human_time_diff function in there instead of the get_the_time. The code I pasted in my very first message works fine.

    Are you suggesting I try

    $cb_date = ‘<i class=”fa fa-clock-o”></i> <time class=”updated” datetime=”‘ . human_time_diff(‘Y-m-d’, $cb_post_id) . ‘”>’ . date_i18n( get_option(‘date_format’), strtotime(human_time_diff(“Y-m-d”, $cb_post_id ))); . ‘</time>‘;

    Thanks

    #190292
    stanmoor
    Participant

    @chrisburton

    Right ok, I thought it was a function that I could insert instead of another date function. The link you gave says:

    human_time_diff( get_the_time(‘U’), current_time(‘timestamp’) )

    Is acceptable, so why would

    $cb_date = ‘<i class=”fa fa-clock-o”></i> <time class=”updated” datetime=”‘ . human_time_diff(get_the_time(‘U’),current_time(‘timestamp’)) . ‘ago’; ‘”>’ . date_i18n( get_option(‘date_format’), strtotime(human_time_diff(get_the_time(‘U’),current_time(‘timestamp’)))) . ‘</time>‘;

    Not work?

    It’s difficult for me because I can’t find anything referring to ‘time’ in any of my theme’s code except for the above. :(

    #190297
    stanmoor
    Participant

    Yes, I’m aware of that, but what I’m saying is I’m finding it difficult to know where to insert this human_time_diff because of the way my theme is designed.

    #190366
    LewisCowles1986
    Participant

    I think I have cracked it for you buddy…
    W3Schools – Time element
    WordPress Codex – get_the_time Function Reference
    PHP Manual – Date Function

    Your Code
    $cb_date = ‘&lt;i class=”fa fa-clock-o”&gt;&lt;/i&gt; &lt;time class=”updated” datetime=”‘ . human_time_diff(get_the_time(‘U’),current_time(‘timestamp’)) . ‘ago'; ‘”&gt;’ . date_i18n( get_option(‘date_format’), strtotime(human_time_diff(get_the_time(‘U’),current_time(‘timestamp’)))) . ‘&lt;/time&gt;‘;
    <code></code>

    should be
    $post_time = get_the_time( 'U' );
    $human_time = human_time_diff( $post_time, time() );
    $cb_date = sprintf(
    '&lt;i class="fa fa-clock-o"&gt;&lt;/i&gt; &lt;time class="updated" datetime="%s"&gt;%s ago&lt;/time&gt;',
    date( 'Y-m-d H:i:s', $post_time ),
    $human_time
    );
    <code></code>

    Gist for testing outside of WP

    #190452
    stanmoor
    Participant

    Hey Lewis

    Thank you SO much!

    I’m 99% sure the code is going to work as I just tried it. However, due to the code coming out weird above, some of the functions aren’t coming out correctly. The description of each post now says:

    <i class=”fa fa-clock-o”></i> <time class=”updated” datetime=”2014-12-08 22:50:48″>22 hours ago</time>

    Is it due to the code above not coming out properly?

    Thanks in advance

    #190453
    stanmoor
    Participant

    Ignore the above! I’ve managed to get the code working. Thanks so much mate – if you’re ever in London I will get you a beer!

    James

    #190488
    LewisCowles1986
    Participant

    Yeah glad you got it working buddy, sorry I switched the date formats on you lol, still it’s all fun and good, and I’m from Essex, Great to see other UK residents on here as well as other nations ;)

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.