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```
Related
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');
}
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 );
I need a query to get all posts that date custom field are before a month sended by URL var. Custom field saves data like YYYYMMDD (20150330). I want to get all post before this year/month (201503).
I try this but it doesn't works, because date_added content it isn't YYYYMM. It contains day too.
$year = $_GET['year'];
$month = $_GET['mes'];
$yearmonth = $month." ".$year;
$args = array (
'post_type' => 'clients',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'post_title',
'meta_query' => array(
array(
'key' => 'client_type',
'compare' => '=',
'value' => 'Si',
),
array(
'key' => 'date_added',
'compare' => '<=',
'value' => date("Ym", strtotime($yearmonth)),
)
));
$posts = get_posts($args);
Anyone can help me with this?
Thanks
If the date is stored in the database as an integer, then look where date less than date("Ym00", strtotime($yearmonth)) (zeros for day) for everything before March-1 or if you wanted the month of march use "Ym99" to signify a date in march that is greater than the last day but still less than the following month. The date 20150301 as an integer translates to 20,150,301 (twenty million, one hundred fifty thousand, three hundred and one). No date will be below 201,503 so you need to add the day numbers to make them in the same scale.
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
I've successfully created the dropdown menu to auto populate the appropriate information from an Advanced Custom Field I created called 'date' on site http://albertson.staging.wpengine.com/seminars/.
Followed along with the instructions here:
http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields
The only issue I'm having is how to display the date in a "pretty" format. You can see that the date is all numbers (20140129) instead of 01/28/2014
To display the date appropriately in the seminar sections above (border red) I use:
<?php if( get_field('date')): ?>
<?php
$date = get_field('date');
// $date = 19881123 (23/11/1988)
// extract Y,M,D
$y = substr($date, 0, 4);
$m = substr($date, 4, 2);
$d = substr($date, 6, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
// format date (November 11th 1988)
echo date('M d', $time);
?>
How do I pass this same information within the Gravity Forms Function I created to get the date to display nicely? Below is my function for Gravity Forms so far.
add_filter('gform_pre_render_4', 'populate_dates');
function populate_dates($form){
foreach($form['fields'] as &$field){
if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-dates') === false)
continue;
// you can add additional parameters here to alter the posts that are retreieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$events = get_posts(array(
'post_type' => 'seminars',
'orderby' => 'date',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'date',
));
// update 'Select a Post' to whatever you'd like the instructive option to be
$choices = array(array('text' => 'Select a Date', 'value' => ' '));
foreach($events as $post){
$choices[] = array('text' => $post->date, 'value' => $post->date);
}
$field['choices'] = $choices;
}
return $form;
}
Sounds like you're looking for PHP's date function. We convert the date string to a timestamp via strtotime() and then use date() to format the date as you wish.
$formatted_date = date( 'm/d/Y', strtotime( $post->date ) );
In your code example:
foreach( $events as $post ){
$formatted_date = date( 'm/d/Y', strtotime( $post->date ) );
$choices[] = array('text' => $formatted_date, 'value' => $post->date );
}