Date field issue with timestamp when the date is before 1970

[ad_1]

Hello!

I’ve found an issue with the date field. My date field has the ‘timestamp’ property set to TRUE.

When saving a date older than January 1st 1970, the date won’t be saved correctly.

For example, after saving the post, a date field set to December 31 1969 will be saved as January 2nd 1970. The date December 1 1969 will be saved as February 1st 1970.

Basically, it’s an issue with how the timestamp is sanitized. Any date prior to January 1st 1970 will have a negative value for the timestamp. However, the sanitize_datetime method does the following when ‘timestamp’ is set to TRUE for the field:

	private function sanitize_datetime( $value, $field ) {
		return $field['timestamp'] ? floor( abs( (float) $value ) ) : sanitize_text_field( $value );
	}

This is obviously an issue. The abs function will automatically convert all negative values to positive values… meaning that any date before 1970 will never be saved correctly.

I know I can set the ‘sanitize_callback’ property to ‘none’ in order to avoid this issue. But I’ve found this solution after losing a lot of time. It seems counterintuitive to me to prevent the users from saving any date prior to 1970 when they are using a timestamp. It’s not explained anywhere.. The date picker allows us to select a date before 1970, and it should save the date correctly out of the box, without having to disable the sanitize_callback method.

In my humble opinion, the abs function should be removed from the sanitize_datetime method. It’s not sanitizing the timestamp, it’s altering it…

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer