I'm trying to add to this if statement another one that checks if post time is before a specific date (let's say 14th March 2014..).
<?php if (in_category('5')) {
echo 'something'; }
?>
I tried with this but it's echoed even on posts newer than that date
<?php
$date1 = "2014-03-14";
$date2 = $post->post_date;
if ( (in_category('5')) && ($date1 < $date2) ) {
echo 'something';
}
?>
I'd convert your dates to timestamps first to make them easier to compare.
global $post;
$compare_date = strtotime( "2014-03-14" );
$post_date = strtotime( $post->post_date );
if ( in_category(5) && ( $compare_date > $post_date ) ) ...
It looks like you have your operator the wrong way round as well. You wanted to check if posts are before the date. The post date would have to be smaller to be before. Also you're passing in category ID 5 as a string for some reason which I corrected in my code above.
Thanks to Nathan, his answer worked great for me.
global $post;
$compare_date = strtotime( "2016-07-14" );
$post_date = strtotime( $post->post_date );
if ( in_category(2) && ( $compare_date >= $post_date ) ) {
echo "On or before July 14."
} else {
echo "After July 14."
}
Related
i need to separate posts older than 2017-07-01.
In loop i'm using these two codes for displaying blogposts.
$compare_date = strtotime("2017-07-01");
$post_date = get_the_date();
and trying to compare them like
if( $compare_date >= $post_date ){
//old
}else{
//new
}
but abviously it didnt work because one of them returns 1498867200
and other one 1 Haziran 2017
I also tried putting strtotime(get_the_date()) but that result is empty.
How can i achieve this?
Get the post_date in the same format as compare_date
Now compare with dates
$compare_date = "2017-07-01";
$post_date = get_the_date( 'Y-m-d' );
if( $compare_date >= $post_date ){
//old
}else{
//new
}
Now compare to time
$compare_date = strtotime("2017-07-01");
$post_date = strtotime(get_the_date( 'Y-m-d' ));
if( $compare_date >= $post_date ){
//old
}else{
//new
}
Not tested, but workable solution.
Ok so I have this code and im not sure exactly how to go about checking if the post modified date was more than a week ago.
So if the post was modified more than a week ago it should echo modified.
The code:
$sticky = get_option( 'sticky_posts' );
if (count($sticky) > 0) {
$stringSticky = implode(",", $sticky);
$postsModifiedCheck = $wpdb->get_results(
"SELECT ID, post_modified
FROM `{$wpdb->prefix}posts`
WHERE post_status = 'publish' AND post_type = 'post'
AND ID IN ($stringSticky)"
);
$now = new DateTime();
$currentDateTime = $now->getTimestamp();
foreach ($postsModifiedCheck as $post) {
if ($currentDateTime > $post->post_modified) {
echo "modified";
}
}
}
So at the moment it will echo "modified", just not sure how to modify the dates to echo "modified" if $post->post_modified more than a week ago.
Cheers
check your formats -
var_dump($currentDateTime .' <- current date - modified date -> '.$post->post_modified );
see if they are the same format,
if they are not the same format, force them to be the same like this
then when you get to that point - just create a variable where you check the difference f.eks
if($currentDateTime - $post->post_modified >= 7){echo 'shiit, It has been modified over a week ago'; }
This should help you on your way
Ok so this is how I solved the problem.
While in the loop use this code:
$weekAgo = strtotime('-1 week');
foreach ($postsModifiedCheck as $post) :
if (intval(strtotime($post->post_modified)) < intval($weekAgo)) unstick_post( $post->ID );
endforeach;
The code will check if the post modified date was more than one week ago and unstick the post.
Hope this helps someone :)
Currently have a list of events in a table I have managed to get them to sort via date the closest date first however I would like past dates to get filtered out and not display. At the minute I am trying to use a if statement to check if the date is more then or equal to the current date but it is not working getting no errors
$repeater = get_field('dates');
$currentdate = date('Ymd');
echo $currentdate;
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['date'];}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ) :
$date = DateTime::createFromFormat('Ymd', $row['date']);
?>
<ul>
<li>
<?php if ($date >= $currentdate) { echo $date->format('d') ;}?><?php endforeach;?></li>
At the bottom of your code there is a bracket ( missing after if.
<?php if $date >= $currentdate)
<?php
$repeater = get_field('dates');
$currentdate = date('Ymd');
foreach($repeater as $dates) {
$date = $dates['date'];
$date = date('Ymd', strtotime($date));
if(strtotime($date) >= strtotime($currentdate)) {
echo 'it is working';
}
}
?>
Let me know if it works
I find it difficult to calculate time difference using PHP. How to calculate time difference in HH:MM:SS format (hours:minutes:seconds) between two different dates?
For example, the input is:
$start_time : 19:30
$end_time : 7:30,
$currenttime = 21:30
I have to find out current time has match with given start time and end time.
Convert the times to a timestamp value, which is an integer value in seconds. Then, you can subtract them easily to get the difference in seconds. After that, it's pretty straightforward to convert that back to hours and minutes.
$iStart = strtotime( '00:00:00' );
$iEnd = strtotime( '12:00:00' );
$iCurrent = strtotime( date('H:i:s') );
if( $iCurrent > $iStart && $iCurrent < $iEnd ) {
echo "active!";
} else {
echo "not active";
}
This will only respect the time (it will be active once a day).
If you need specific dates you can use:
$iStart = strtotime( '2012-12-01 00:00:00' );
$iEnd = strtotime( '2012-12-31 12:00:00' );
$iCurrent = time();
if( $iCurrent > $iStart && $iCurrent < $iEnd ) {
echo "active!";
} else {
echo "not active";
}
I'm going to create a "future"-blogg (sort of a sci-fi-adventure in blog form) and want to display all dates +100 years. For instance a post published 2012-05-17 should display the date 2112-05-17.
First I thought I could just easily set the date to 2112-05-17, but it seems that wordpress can't handle dates higher than 2049.
So my next idea is to modify how the dates are displayed. I was thinking of modifying get_the_date() in general-template.php, and make it return the later date.
But here my skills are not enough. I don't know anything about how to work with date values in php.
get_the_date() looks like this:
function get_the_date( $d = '' ) {
global $post;
$the_date = '';
if ( '' == $d )
$the_date .= mysql2date(get_option('date_format'), $post->post_date);
else
$the_date .= mysql2date($d, $post->post_date);
return apply_filters('get_the_date', $the_date, $d);
}
Any ideas on how to modify it? So it adds 100 years to the date before returning it?
Any input would be appriciated :)
Looks like you might need to investigate date_modify and also strtotime
http://php.net/manual/en/datetime.modify.php
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/datetime.add.php
Assuming your mysql dates are of the following format: YYYY-MM-DD
function add100yr( $date="2011-03-04" ) {
$timezone=date_timezone_get();
date_default_timezone_set($timezone);
list($year, $month, $day) = split(':', $date);
$timestamp=mktime(0,0,0, $month, $day, $year);
// 100 years, 365.25 days/yr, 24h/day, 60min/h, 60sec/min
$seconds = 100 * 365.25 * 24 * 60 * 60;
$newdate = date("Y-m-d", $timestamp+$seconds );
// $newdate is now formatted YYYY-mm-dd
}
Now you can:
function get_the_date( $d = '' ) {
global $post;
$the_date = '';
if ( '' == $d )
$the_date .= mysql2date(get_option('date_format'), add100yr($post->post_date));
else
$the_date .= mysql2date($d, add100yr($post->post_date));
return apply_filters('get_the_date', $the_date, $d);
}
Try a custom field: http://codex.wordpress.org/Custom_Fields
You will have to enter the +100 year date for each post, but then you're not going to be relying on php or functions to alter the current date.
WordPress provides the filter get_the_date that allows to modify the value before it is handled over to the theme or plugin.
This filter is used everytime get_the_date() is called.
add_filter( 'get_the_date', 'modify_get_the_date', 10, 3 );
function modify_get_the_date( $value, $format, $post ) {
$date = new DateTime( $post->post_date );
$date->modify( "+100 years" );
if ( $format == "" )
$format = get_option( "date_format" );
return( $date->format( $format ) );
}
This function takes the post_date from the post, adds the time and returns it according to the format given to get_the_date() or with the default format configured in the WordPress options.