• Resolved Guido

    (@guido07111975)


    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 strtotime is adding this for me.
    I have noticed the strtotime causes 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)
  • Moderator bcworkz

    (@bcworkz)

    Hiya Guido!

    I don’t think it would literally conflict, but it could cause undesirable behavior elsewhere. My advice would be to first get the current default before changing it, then restore it when your callback is finished. Better safe than sorry πŸ™‚

    Thread Starter Guido

    (@guido07111975)

    Hi there BC!

    But how to restore it to the previous one, because that one could be every timezone?

    Update: restore to default with this perhaps?

    
    date_default_timezone_set(ini_get('date.timezone'));
    

    Update 2: the above would always be UTC or not? So not always the “previous” timezone.

    Guido

    Thread Starter Guido

    (@guido07111975)

    Hi again BC πŸ™‚

    Get I’ve found a proper solution here.

    So my example becomes this:

    
    function save_event_info( $post_id ) {
    	// get current timezone
    	$current_zone = date_default_timezone_get();
    	// set utc timezone for strtotime
    	date_default_timezone_set('UTC');
    	update_post_meta( $post_id, 'date', sanitize_text_field(strtotime( $_POST['date'] ) ) );
    	// back to previous timezone
    	date_default_timezone_set($current_zone);
    }
    add_action( 'save_post', 'save_event_info' );
    

    Looks fine to you as well?

    Guido

    Moderator bcworkz

    (@bcworkz)

    Yes! That is exactly what I had in mind πŸ™‚

    FWIW, after I last replied here I happened to see a note in a discussion of the new timezone handling functions stating that all sites should have date_default_timezone_set('UTC'); to start with, implying that it’s safe to leave it set to UTC and there is no need to save/restore the original setting. But I strongly believe in leaving settings the way I found them any time I have a need to change a specific value.

    Thread Starter Guido

    (@guido07111975)

    I had a conflict with another plugin (not listed at wp dot org). That plugin changed the default timezone from UTC to the local timezone (set at Settings > General). In my case this resulted in the wrong timestamp was being saved in datebase.

    Can you give me the URL of that discussion, because I will contact the plugin developer about this.

    Thanks, BC! Have a nice rest of your weekend.

    Guido

    Thread Starter Guido

    (@guido07111975)

    ..I happened to see a note in a discussion..

    Guess at the Make WP Core page?

    Guido

    Moderator bcworkz

    (@bcworkz)

    That’s a good page to send them to. Though the UTC requirement is a little vague after a quick read. I may have missed it. I’d also send them to https://wordpress.org/support/topic/read-this-first-wordpress-5-3-master-list/#post-12124062

    Thread Starter Guido

    (@guido07111975)

    Great, off by one day was the case here as well. I now fully understand what happened.

    Thanks BC!

    Guido

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Using strtotime when saving date value’ is closed to new replies.