Title: Using strtotime when saving date value
Last modified: December 13, 2019

---

# Using strtotime when saving date value

 *  Resolved [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/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 `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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12236905)
 * 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](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12236932)
 * 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](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12238062)
 * Hi again BC 🙂
 * Get I’ve found a proper solution [here](https://stackoverflow.com/questions/37503048/set-time-zone-for-strtotime-function).
 * 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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12238909)
 * 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](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12239126)
 * 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](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12239996)
 * > ..I happened to see a note in a discussion..
 * Guess at the [Make WP Core](https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/)
   page?
 * Guido
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12240659)
 * 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](https://wordpress.org/support/topic/read-this-first-wordpress-5-3-master-list/#post-12124062)
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12240766)
 * 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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 8 replies
 * 2 participants
 * Last reply from: [Guido](https://wordpress.org/support/users/guido07111975/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/using-strtotime-when-saving-date-value/#post-12240766)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
