- This topic is empty.
-
AuthorPosts
-
December 8, 2014 at 2:08 am #190263stanmoorParticipant
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
December 8, 2014 at 7:03 am #190285stanmoorParticipant@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.
December 8, 2014 at 7:41 am #190288stanmoorParticipantHi 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
December 8, 2014 at 9:00 am #190292stanmoorParticipantRight 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. :(
December 8, 2014 at 9:30 am #190297stanmoorParticipantYes, 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.
December 8, 2014 at 8:49 pm #190366LewisCowles1986ParticipantI think I have cracked it for you buddy…
W3Schools – Time element
WordPress Codex – get_the_time Function Reference
PHP Manual – Date FunctionYour Code
$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>‘;
<code></code>should be
$post_time = get_the_time( 'U' );
$human_time = human_time_diff( $post_time, time() );
$cb_date = sprintf(
'<i class="fa fa-clock-o"></i> <time class="updated" datetime="%s">%s ago</time>',
date( 'Y-m-d H:i:s', $post_time ),
$human_time
);
<code></code>December 9, 2014 at 1:45 pm #190452stanmoorParticipantHey 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
December 9, 2014 at 1:48 pm #190453stanmoorParticipantIgnore 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
December 10, 2014 at 2:48 am #190488LewisCowles1986ParticipantYeah 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 ;)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.