Difficulty changing date format in post meta value

admin2025-05-31  0

I have a custom post type with a custom field that takes a date. The posts are later ordered by the date in that custom field. Unfortunately I formatted the date incorrectly and there are already a number of posts with the post meta value as dd-mm-yyyy. I have found that to use this value as orderby (WPQuery) it needs to be yyyy-mm-dd. So I was going to try:

$args = array(
            'posts_per_page' => -1, 
            'post_type'=> 'match_report'
            );
            $wp_query = new WP_Query(); 
            
            $wp_query->query($args); 
                        
if ( $wp_query->have_posts() ) :                

while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
  
$matchdate = get_post_meta($post->ID, 'report_date', true);         
$new_matchdate = strftime("%Y-%m-%d", $matchdate);          
update_post_meta($post->ID, 'report_date', $new_matchdate); 
   
endwhile;
  
wp_reset_postdata();

endif; ?>

but I am getting

Notice: A non well formed numeric value encountered in...

and when I echo $new_matchdate I get 1970-01_01 so obviously I am doing this incorrectly. Any help would be appreciated.

I have a custom post type with a custom field that takes a date. The posts are later ordered by the date in that custom field. Unfortunately I formatted the date incorrectly and there are already a number of posts with the post meta value as dd-mm-yyyy. I have found that to use this value as orderby (WPQuery) it needs to be yyyy-mm-dd. So I was going to try:

$args = array(
            'posts_per_page' => -1, 
            'post_type'=> 'match_report'
            );
            $wp_query = new WP_Query(); 
            
            $wp_query->query($args); 
                        
if ( $wp_query->have_posts() ) :                

while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
  
$matchdate = get_post_meta($post->ID, 'report_date', true);         
$new_matchdate = strftime("%Y-%m-%d", $matchdate);          
update_post_meta($post->ID, 'report_date', $new_matchdate); 
   
endwhile;
  
wp_reset_postdata();

endif; ?>

but I am getting

Notice: A non well formed numeric value encountered in...

and when I echo $new_matchdate I get 1970-01_01 so obviously I am doing this incorrectly. Any help would be appreciated.

Share Improve this question edited May 23 at 20:23 Glorfindel 6113 gold badges10 silver badges18 bronze badges asked Jan 29, 2014 at 10:32 mantismantis 7902 gold badges18 silver badges38 bronze badges 1
  • Try with strtotime check this might helpful.. [stackoverflow/questions/11622755/… [1]: stackoverflow/questions/11622755/… – Sriram Sri Commented Jan 29, 2014 at 10:53
Add a comment  | 

1 Answer 1

Reset to default 2

Oh my. You're doing some freaky stuff there. :)

Please try it like this:

$args = array(
    'posts_per_page' => -1,
    'post_type'=> 'match_report',
);
$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        $matchdate = get_post_meta(get_the_ID(), 'report_date', true);
        $new_matchdate = DateTime::createFromFormat('d-m-Y', $matchdate);
        update_post_meta(get_the_ID(), 'report_date', $new_matchdate->format('Y-m-d'));
    }

    wp_reset_postdata();
}

// EDIT
If you have problems doing it the object oriented way, try it procedural like so:

$args = array(
    'posts_per_page' => -1,
    'post_type'=> 'match_report',
);
$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        $matchdate = get_post_meta(get_the_ID(), 'report_date', true);
        $new_matchdate = date_create_from_format('d-m-Y', $matchdate);
        update_post_meta(get_the_ID(), 'report_date', date_format($new_matchdate, 'Y-m-d'));
    }

    wp_reset_postdata();
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748626814a313623.html

最新回复(0)