1

I'm trying to compare times like:

if this post was posted between the past 6months show human_time_diff() else show the_time('j. M .Y')

my idea was:

function time_ago() {
  global $post;
  $now = time();
  $post_created = strtotime($post->post_date);
  $sixMonthsAgo = 180*24*60*60;
  $human_time = 'hace '. human_time_diff( get_the_time('U'), current_time('timestamp') );
  $mobile = wp_is_mobile();


  if (( $now - $post_created ) > $sixMonthsAgo && $mobile) {
   the_time('j. M .Y');

 } elseif(( $now - $post_created ) > $sixMonthsAgo && !$mobile) {
   the_time('j. F .Y');

 } else {
   echo $human_time;

 }

}

What I pretend to do with this function is comparing the date when the post was published and because time() is in seconds I compare seconds with seconds in $sixMonthsAgo

But this only shows the human_time_diff();

no more ideas ...

what do you think ? where my logic is wrong?

thanks!

1

Here is how it works for me with minor changes:

  global $post;
  $post_created = strtotime($post->post_date);
  $sixMonthsAgo = strtotime('-6 months');
  $human_time = 'hace '. human_time_diff( get_the_time('U'), current_time('timestamp') );
  $mobile = wp_is_mobile();

  if ($post_created > $sixMonthsAgo && $mobile) {
   the_time('j. M .Y');

 } elseif( $post_created > $sixMonthsAgo && !$mobile) {
   the_time('j. F .Y');

 } else {
   echo $human_time;

 }

I hope this simplification helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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