Display last date a post was updated on WordPress - php

I'm using some PHP to display the last time a blog post was updated on WordPress using the get_the_time and get_the_modified_time functions. However, I can't get the last modified time to display inline in the paragraph.
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
Here's a screenshot of the result:

the_modified_time prints the last modification time, so it prints it before you print your <p>.
Instead you'll want to use get_the_modified_time for setting $t, like so:
$t = get_the_modified_time('F d, Y');

It's probably because these functions don't return the result but echo it directly. Thus you must call them at the spot you need them to be.

<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
echo "<p class=\"lastupdated\">Updated on ".the_modified_time('F d, Y')."</p>";
}
?>

You can also display the last modified time of a post through this code by placing it anywhere in your loop.
Posted on <?php the_time('F jS, Y') ?>
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if($u_modified_time != $u_time) {
echo "and last modified on ";
the_modified_time('F jS, Y');
echo ". ";
} ?>

This is the complete code based on the above comments. Now it works and you just need to add this to your post template: Snippet (1)
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = get_the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
For example, suppose that your post template is called content-post.php. Look for a part that looks like the below: Snippet (2)
// Post date
if ( in_array( 'post-date', $post_meta_top ) ) : ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php endif;
Insert snippet (2) immediately before the closing tag of snippet (1) as follows:
<?php
// Post date
if ( in_array( 'post-date', $post_meta_top ) ) : ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = get_the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
<?php endif;
Now, you will get the original post date and the updated post date. Special thanks go to #Sebastian Paaske Tørholm for his comment.

Related

I need to change two words to arabic words in wordpress theme

I need to change "Last updated on" and "Posted on" From the code below to another words in arabic.
This code is part of the "template-tags.php" file which is part of the WordPress theme files
I just tried replacing the words but it didn't work for me
It just looks like this"����������"
<?php if ( get_theme_mod('show_post_date', '1') == 1 ) : ?>
<div class="entry-date"><i class="fa fa-clock-o"></i><span >
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 60) {
echo "Last updated on "; // here
the_modified_time('Y-m-d');
}
else {echo "Posted on "; // here
the_time('Y-m-d');
} ?>
There seems to be no problem with the code. You can apply these changes to see if it works.
Using htmlentities
<?php if (get_theme_mod('show_post_date', '1') == 1) : ?>
<div class="entry-date"><i class="fa fa-clock-o"></i><span>
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 60) {
$text = 'آخر تحديث في';
echo htmlentities($text, ENT_QUOTES, "UTF-8"); // here
the_modified_time('Y-m-d');
} else {
$text = 'نشر على';
echo htmlentities($text, ENT_QUOTES, "UTF-8"); // here
the_time('Y-m-d');
} ?>
<?php
endif;
Notice that I
<? Php
endif;
I added myself, it will probably be different from your code and still continue.
Or you can use utf8_encode like this:
<?php if (get_theme_mod('show_post_date', '1') == 1) : ?>
<div class="entry-date"><i class="fa fa-clock-o"></i><span>
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 60) {
$text = 'آخر تحديث في';
echo utf8_encode($text); // here
the_modified_time('Y-m-d');
} else {
$text = 'نشر على';
echo utf8_encode($text); // here
the_time('Y-m-d');
} ?>
<?php
endif;
And if the above did not work.
You should check to see if there is a UTF-8 meta on your page. <meta charset="utf-8"> This meta should be in your header.php file. If not, add it.
If you did all the work but did not answer, use a standard Arabic or Persian font

displaying icon if date created and local date are the same php

<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat =$createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (servermonth == newdateformat) {
?>
<span class="label label-default">New</span>
<?php
}
?>
basically it should show the "New " icon when the date is created today. they are both echo-ing the same date but the span is not showing up
Try using this:
if (strtotime(servermonth) === strtotime(newdateformat)) {
echo '<span class="label label-default">New</span>';
}
<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat =$createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (strtotime($servermonth) == strtotime($newdateformat)) { ?>
<span class="label label-default">New</span>
<?php } ?>
It should be:
<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat = $createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (strtotime($sservermonth) == strtotime($newdateformat)) {
echo '<span class="label label-default">New</span>'
}
?>

How to add class in a string in PHP

I would like to put class inside this string :
<div class="date-custom-page">
<?php
$date = get_field('jour_de_levenement', false, false);
$time = strtotime($date);
?>
<?php $dateformatstring = "<p class='date-event'>j</p><p class='month'>F Y</p>"; ?>
<?php $unixtimestamp = strtotime(get_field('jour_de_levenement', false, false));
echo date_i18n($dateformatstring, $unixtimestamp); ?>
I have tried this but only the p is taken, not the class.
Can someone help me ?
Thanks a lot !
Just use this :
$dateformatstring = "<p class='date-event'>j</p><p class='month'>F Y</p>";

Get value from data-attribute and use it in a filter

This is puzzling me for the past hours already. Hopefully anybody has the clearing answer and in any case, thank for any support. In need to filter between three values in a data-attribute, which are set in jQuery. These values are changed and put in the attribute, depending on screenwidth. The jQuery part works fine. Now the tricky part is that in the PHP file the right value of the data-width attribute should be used and put as a value for a custom field. This value triggers the right images in a backgroundslider.
I tried the if/else statement in which the attribute value had a variable, which could give the right value for the custumfield, when its value would be equal as the one mad in jQuery. This failed. My knowledge is limited and therefore my files looked like this.
jQuery:
function schermFilter() {
var scherm = $(window).width();
if (scherm >= 320 && scherm <= 480) {
$('#homeslider-wrapper').attr('data-width','slides_mobiel')
console.log(scherm + ": mobiel");
} else if (scherm >= 768 && scherm <= 992) {
$('#homeslider-wrapper').attr('data-width','slides_tablet')
console.log(scherm + ": tablet");
} else {
$('#homeslider-wrapper').attr('data-width','slides_groot')
console.log(scherm + ": groot");
}
};
schermFilter();
$(document).ready(function() {
schermFilter();
});
PHP:
<?php $screenwidth =""; ?>
<?php $args = array(
'post_type' => 'homeslides',
'order' => 'date'
);
$slider = new WP_QUERY($args);
if($slider->have_posts()) : ?>
<div id="homeslider-wrapper" class="slides" data-width="$screenwidth">
<?php while ($slider->have_posts()) : $slider->the_post(); ?>
<?php if ($screenwidth === "slides_mobiel") {
$image = get_field('slides_mobiel');
echo $screenwidth;
} elseif ($screenwidth === "slides_tablet") {
$image = get_field('slides_tablet');
echo $screenwidth;
} else if ($screenwidth === "") {
$image = get_field('slides_groot');
echo $screenwidth;
} else {
$image = get_field('slides_groot');
echo "groot - else";
}
?>
<?php $imageList .= '"'. $image['url'] . '",'; ?>
<?php endwhile; ?>
</div>
<?php endif;wp_reset_postdata(); ?>
<?php $imageList = rtrim($imageList, ','); ?>
I tried isset and property-exists, but without success. Thanks!

Simple PHP question

Yes, it's a simple question, but one that I can't find a answer for through the PHP documentation or Google. (I'm just learning PHP....)
If this works:
<?php $d=date("D"); if ($d="Mon") { ?>echo this text on Monday<?php endwhile; ?><?php } else { ?><?php } ?>
Why doesn't this?
<?php $d=date("D"); if ($d="Mon,Tue") { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>
Do I need different delimiters between Mon and Tue? I've tried || and && ....
Thanks, Mark
You're performing an assignment of $d when you say ($d="Mon"). What you want is the comparison operator (==):
if ($d == "Mon" || $d == "Tue")
You're assuming that date("D") will return more than one value. It will only return the current day. Instead use this:
<?php $d=date("D"); if (in_array($d, array("Mon","Tue"))) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>
The string $d is either going to contain "Mon" or "Tue", never "Mon,Tue". You can't compare strings this way. You need to use an expression like this:
if ($d == "Mon" || $d == "Tue") {
try:
<?php $d=date("D"); if (in_array($d,array('Mon','Tue'))) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>
Maybe this:
if ($d == "Mon" || $d == "Tue") {
also, php has two operators for equality.
== and ===
If you've string with value 'Mon,Tue', Then you can check
if($d=='Mon,Tue')
There is no chance for that, So you need to use OR condition.
ie,
if($d=='Mon' || $d=='Tue')
Try this code which is more user readable:
<?php $d=date("D");
$days=array("Mon,Tue");
if ($d="Mon,Tue") if(in_array($a,$days)) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

Categories