$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Difficulty changing date format in post meta value|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Difficulty changing date format in post meta value

matteradmin7PV0评论

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();
}
Post a comment

comment list (0)

  1. No comments so far