Using strtotime when saving date value
-
Hi,
I have an events plugin that stores the date in database with this function:
function save_event_info( $post_id ) { update_post_meta( $post_id, 'date', sanitize_text_field(strtotime( $_POST['date'] ) ) ); } add_action( 'save_post', 'save_event_info' );My plugin does not have input field for time, so the
strtotimeis adding this for me.
I have noticed thestrtotimecauses unexpected behavior, depending on the timezone. In some cases this results into the wrong day being returned after saving the value.So now I’m setting a default timezone before saving:
function save_event_info( $post_id ) { date_default_timezone_set('UTC'); update_post_meta( $post_id, 'date', sanitize_text_field(strtotime( $_POST['date'] ) ) ); } add_action( 'save_post', 'save_event_info' );Can this default timezone cause a conflict with other parts of my website, although the function is only being executed while saving the post? I’m not completely certain..
Guido
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
The topic ‘Using strtotime when saving date value’ is closed to new replies.