0

I have a custom field to store the date of games, and I store it in database like

mm/dd/yyyy

Now I need to convert all dates to

dd/mm/yyyy

So how can do that?

Is timestamp a solution for it, if yes, how can do that

I need to change all old data to new data for 200 posts

  • Are you talking about custom fields? If so, you should store dates as yyyy/mm/dd – Pieter Goosen Feb 29 '16 at 10:45
  • yes, before i store as yyyy/dd/mm but now i change it to yyyy/mm/dd, but old data i need to change it – mak man Feb 29 '16 at 10:50
  • Use date() in conjunction with strtotime(). Click – dingo_d Feb 29 '16 at 10:50
  • @dingo_d how can change old data ? – mak man Feb 29 '16 at 10:52
  • $new_date = date('d/m/Y', strtotime($old_time));, where $old_time is the value of your date from where ever you are pulling it from (database I presume). – dingo_d Feb 29 '16 at 10:57
1

Find the correct key for game_date_key and run this once:

global $wpdb;
$posts = $wpdb->get_results("SELECT ID FROM ".$wpdb->prefix."posts" WHERE post_type='post');
foreach ($posts as $post) {
    $old_date = get_post_meta($post->ID,'game_date_key',true);
    $new_date = date('d/m/Y', strtotime($old_date));
    update_post_meta($post->ID,'game_date_key',$new_date,$old_date);
}

(I assume you will have the sense to backup your database first just in case. :-)

  • nice.. but some time i store in mm/dd/yyyy what can i do ? – mak man Feb 29 '16 at 18:22
  • pick one format and stick to it (the new is more international), you can always convert it back a similar way after retrieving it if need be. – majick Mar 1 '16 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.