Support

Account

Home Forums Backend Issues (wp-admin) Remaining Days Calculation

Helping

Remaining Days Calculation

  • I have a custom post type called Contracts and each contract has an expiry date (ACF date picker). I am trying to calculate the remaining days of expiry and display it on a custom field.

    Here is my code. When I run the cron job, it shows the remaining days as 19213 for a contract with expiry date of 31/12/2022. Can anyone guide me on how to correctly display the remaining days?

    function wpf11144133_contracts_expiry_time() {
    
    $date_to    = get_field( 'valid_until');
    $date_expiry     = new DateTime(date('Y-m-d ', strtotime($date_to)));
    $date_today = new DateTime(date('Y-m-d ', strtotime('today')));
    
    $days = $date_expiry->diff($date_today)->days;
       
    	$posts = get_posts( array(
    		'post_type'			=> 'contract',	// Contract post type
    		'post_status' 		=> 'publish',	// Only grab published posts
    		'posts_per_page'	=> -1,			// Grab all posts
    		'no_found_rows'		=> true,		// Don't calculate pagination, we don't need it
    		'update_cache'		=> false,		// Don't need to cache results
    		'meta_query'		=> array(
    			array(
    				'key'		=> 'status',
    				'value'		=> 'Valid',
    				'compare'	=> '=',	
    			)
    			),
    	) );
    	if( ! empty ( $posts ) ) {
    
    		foreach( $posts as $post ) {
    
    			update_field('remaining_days',$days, $post->ID);
    
    		}
    	}
    }
    add_action( 'wpf11144133_contracts_expiry_time_calculation_job', 'wpf11144133_contracts_expiry_time' );
  • The only thing that I can think of that would cause it to not work is the return format of the date field. What do you have that set to?

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.