0

I have a meta field that is mixed with MM/dd/yy hh:mm dates and Unix timestamp dates. How do I convert all of the MM/dd/yy hh:mm dates in the db to Unix? I have only limited PHP and SQL knowledge so be gentle. Thanks!

1 Answer 1

0

strtotime is php function that can be used for this purpose. strtotime — Parse about any English textual datetime description into a Unix timestamp

  1. Firstly get all the posts using meta key .
  2. Then get date value from meta field.
  3. Then convert to unix timestamp.
  4. Then update again.
$args = array( 'posts_per_page' => -1, 'meta_key' => 'meta_key_name' );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); 
         $date = get_post_meta(get_the_id(), 'meta_key_name', true);

         if( strtotime($date) != false ){ // To exclude dates which are already in timestamp
            update_post_meta(get_the_id(), 'meta_key_name', strtotime($date));
          }
    endforeach; 
wp_reset_postdata();?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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