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
strtotime is php function that can be used for this purpose. strtotime — Parse about any English textual datetime description into a Unix timestamp
- Firstly get all the posts using meta key .
- Then get date value from meta field.
- Then convert to unix timestamp.
- 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();?>