Order created time difference - php

I found that code here and I'm wondering how can I add example: if order created time > order created time + 5 minutes?
Now code just check today datetime and order created datetime and make datediff, but I want to make time diff.
If someone can help me, thanks already!
function myplugin_cancel_unpaid_wc_orders() {
global $myplugin_options;
$my_cancel_time = $myplugin_options['myplugin_cancel_time'];
//$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $myplugin_cancel_time ) . ' MINUTES', current_time( 'timestamp' ) ) );
$query = ( array(
'limit' => 5,
'orderby' => 'date',
'order' => 'DESC',
'status' => array( 'pending' )
) );
$orders = wc_get_orders( $query );
foreach ( $orders as $order ) {
$date = new DateTime( $order->get_date_created() );
$today = new DateTime();
$interval = $date->diff($today);
$datediff = $interval->format('%a');
if ( $datediff >= 4 ) {
$order->update_status('cancelled', 'Cancelled for missing payment');
}
}
}

Convert the order created date to seconds. 5 minutes equal 300 seconds.
$order_created_date = $order->get_date_created(); // Get order date created WC_DateTime Object
$order_created_seconds = $order_created_date->getTimestamp(); // Get order create date in seconds
$end_time = $order_created_seconds+300; // Order created time + 5mins
$current_time = time();
if ($current_time >= $end_time) { // Check if end time
$order->update_status('cancelled', 'Cancelled for missing payment');
}

Related

Count Posts Between Dates

I use a few ACF date fields to basically provide start and end dates for posts, I need to be able to display how many posts are starting in the current month and ending in the current month, this is what I have so far, this doesn't provide the accurate count, not sure what's wrong with this and if it could be simplified. I have confirmed the function portion below is working correctly.
function getBetweenDates($startDate, $endDate) //array of dates for current month
{
$rangArray = [];
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
for ($currentDate = $startDate; $currentDate <= $endDate;
$currentDate += (86400)) {
$date = date('Y-m-d', $currentDate);
$rangArray[] = $date;
}
return $rangArray;
}
$this_month = getBetweenDates(date("Y-m-01"), date("Y-m-t"));
$posts = get_posts(array(//start dates this month
'post_type' => 'project',
'meta_key' => 'landlord_delivery_date',
'meta_value' => $this_month,
'meta_compare' => 'IN',
));
$fpposts = get_posts(array(//end dates this month
'post_type' => 'project',
'meta_key' => 'first_patient_date',
'meta_value' => $this_month,
'meta_compare' => 'IN',
));
$count = count($posts);//count ll dates
$fpcount = count($fpposts);//count fp dates```

Filter Gravity Forms Entries By Date

I am trying to filter (show) Gravity Forms Entries on the front end of my site between two certain dates. One being today (the current present date) and a custom date added within the backend of the website.
So only entries that were submitted between the custom date and the present date.
I have two variables storing these dates:
$new_start_date is the custom date, and
$end_date is the present days' date.
My code is outputting 200 entries but some of which are before the custom start date.
Below is my query code:
<?php
//Get the Form ID to get the entries from and store it within a varibale
$form_id = GFAPI::get_form(2);
// Get Dates
$date_page_id = get_field( 'update_dates_page_link', 'options', false, false );
$start_date = get_field( 'oak_submission_date', $date_page_id ); // date of the last submission date
$end_date = date( 'Y-m-d', time() ); //get today's date
// Convert start_date to match end_date format
$new_start_date = DateTime::createFromFormat( 'd/m/Y', $start_date );
$new_start_date = $new_start_date->format( 'Y-m-d' );
$search_criteria = array(
'status' => 'active',
'start_date' => $new_start_date,
'end_date' => $end_date,
'field_filters' => array(
array(
'key' => '53', // Trust name field
'value' => 'Oak Trust',
),
array(
'key' => '49', // Grant Made field
'value' => 'No',
),
),
);
$sorting = null;
$paging = array( 'offset' => 0, 'page_size' => 200 );
$entries = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging );
// Code that outputs to the screen
foreach ( $entries as $entry) :
echo '<li>';
echo 'ID: '. $entry[68] .' : ' . $entry[2] .' : Submission Date: '. $entry[54];
echo '</li>';
endforeach;
GFAPI::get_form(2) doesn't return the Form ID it returns the Form. Try this:
$entries = GFAPI::get_entries( 2, $search_criteria, $sorting, $paging );

categories the posts month wise in wordpress?

i am trying to retrieve the last 8 months posts in wordpress. i have used following code to do this
$args = array(
'posts_per_page' => -1,
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '5 month ago',
)
)
);
$query = new WP_Query( $args );
Now i am wanted to place each month posts in separate array so that i can show them month wise in front-end.
Any idea about this ??
You can use a for loop:
$date = date('Y-m-d');
$dateArray = array();
$dateArray[] = $date;
for($i=1; $i<8 ; $i++){
$month = '- ' . $i . 'month';
$old_date = strtotime ( $month , strtotime ( $date ) ) ;
$old_date = date('Y-m-d', $old_date);
$dateArray[] = $old_date;
}
foreach($dateArray as $item){
$month_loop = date("m", strtotime($item));
$year_loop = date("Y", strtotime($item));
$query = new WP_Query('year=' . $year_loop . '&monthnum=' . $month_loop );
// Do action
}
I think it help to you

wp_insert_post auto schedule or post based on date

<?php
//today date 2014-07-27
$title="title";
$content="content";
$date="2014-07-30";
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'post',
'post_date' => $date,
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
?>
how to make post auto post when the date today (or older day) and auto schedule when the date tomorrow ?
ps: for today (or older day) maybe that code will work correctly but how to do it for auto schedule when i put the date tomorrow or next day (if it possible please give sample code)
update question: what i mean is how to make wp_insert_post make a schedule post when the date set to the future (next day/next month/next year or specific date) because when i try to set 'post_date' => "2015-08-30" the post keep post with today date what i want is that post will create as schedule post
thanks
To test if the date is for tomorrow, try something like this :
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
And for the day after tomorrow :
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
Then you can test $date:
if ($date == $tomorrow) {
echo "tomorrow";
} elseif ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Updated answer :
Basically post_date is for the time post was made and if you want hack i think that you need to have date in this format : [ Y-m-d H:i:s ]
The post datetime is [ Y-m-d H:i:s ], try it:
<?php
$timezone_offset = get_option( 'gmt_offset' );
$post_date = date_create( "2014-07-30" );
$post_date_gmt = date_create( "2014-07-30" );
$post_date_gmt->add( new DateInterval( "PT{$timezone_offset}H" ) );
//today date 2014-07-27
$title="title";
$content="content";
$date="2014-07-30";
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'post',
'post_date' => $post_date->format('Y-m-d H:i:s'),
'post_date_gmt' => $post_date_gmt->format('Y-m-d H:i:s'),
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
?>
Enjoy your code

In Wordpress, can I use WP_Query with timestamp?

I have code like this,
$date_arg = array(
'after' => array(
'year' => $num_days_ago['year'],
'month' => $num_days_ago['mon'],
'day' => $num_days_ago['mday'],
),
'inclusive' => false,
);
$query = new WP_Query( array ( 'date_query' => $date_arg, 'post_type' => $post_type ...
This is working fine but I have to specify a exact date. Is it possible to use "timestamp" to make the query.
For example, I want to query posts that is within 24 hours. I can get the timestamp by
$ts = time() - DAY_IN_SECONDS
It is possible to get the posts that is created after $ts?
You could try constructing a date using the date() and strtotime() functions.
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
I'd also recommend taking a look at this handy website - http://www.viper007bond.com/tag/wp_date_query/
IMHO better like so :
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
and more general :
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last x days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";// here 30
return $where;
}
add_filter( 'posts_where', 'filter_where' ); // add the filter before setting object or before loop
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' ); // .. and do not forget to remove it
Or you could try something like this
SELECT post_title, IF (post_date_gmt < (DATE_SUB(CURDATE(), INTERVAL 24 HOUR)), 'false', 'true') AS A FROM wp_posts
WHERE post_type = "post" AND post_status = "publish"
http://www.w3schools.com/sql/func_date_sub.asp

Categories