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.
Related
I have created a PHP function that calculates how old a WordPress/WooCommerce order is. If the order is older than 90 days it should be canceled. The function used to work perfectly. However, since the new year 2020, it has stopped working. I would assume it's because the function gets confused about the year since -90 days from today is year 2019. How can I make the calculation work with the past years/2019?
I have tried playing with different date formats from the WordPress codex instead of mdy. However, this doesn't seem to do any difference.
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("m/d/y");
// set time to expire
$time_to_expire = "-90 days";
$expiration_date = date("m/d/y", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)) foreach ($result as $order){
// Get order's time
$order_time = get_the_time('m/d/y', $order->ID );
// Compare order's time with current time
if ( $order_time < $expiration_date ){
// Update order status
$orders = array();
$orders['ID'] = $order->ID;
$orders['post_status'] = 'wc-cancelled';
wp_update_post( $orders );
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );
You can simplify this a lot by running an UPDATE query with a WHERE clause, to only fetch those orders that are older than 90 days. No need to fetch them all and loop over the results.
You will need to set the post_created to the actual name of your column.
function expire_after_x_days() {
global $wpdb;
$result = $wpdb->query("UPDATE $wpdb->posts
SET post_status = 'wc-cancelled'
WHERE post_type = 'shop_order'
AND post_status = 'wc-processing'
AND post_created < DATE_SUB(NOW(), INTERVAL 90 DAY)");
}
You are treating those variables as DateTime instances, but they are strings. This $order_time < $expiration_date compares the strings alphabetically, not by their date meaning. Use DateTime class (https://www.php.net/manual/en/class.datetime.php) instead.
Please change date format from m/d/y to Y-m-d. Please see below code.
you can also check manually by modify $order_time = '12/11/18';
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("Y-m-d");
// set time to expire
$time_to_expire = "-90 days";
$expiration_date = date("Y-m-d", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)){
foreach ($result as $order){
// Get order's time
$order_time = get_the_time('Y-m-d', $order->ID );
// Compare order's time with current time
//$order_time = '12/11/18';
if ( $order_time < $expiration_date ){
//die("olde");
// Update order status
$orders = array();
$orders['ID'] = $order->ID;
$orders['post_status'] = 'wc-cancelled';
wp_update_post( $orders );
}else{
//echo 'not old date';die;
}
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );
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'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."
}
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.