cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 83 forks source link

Use current_time() for curdate() #200

Closed rilwis closed 5 years ago

cabrerahector commented 5 years ago

Hi @rilwis,

Thanks for contributing!

However, I noticed that the datetime returned by current_time( 'Y-m-d H:i:s', true ) is actually different from the local time returned by the current implementation:

/* Current implementation, modified to display datetime instead for this example */
echo gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );

// Displays '2018-10-28 16:54:27', which is the current local time
// as set in Settings > General > Timezone.

/* Your code, also modified to display datetime instead of date */
echo current_time( 'Y-m-d H:i:s', true );

// Displays '2018-10-28 20:57:33', which it's not right.

If we pass 0/false as the second parameter to the current_time() function, we get:

echo gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
// '2018-10-28 17:03:22'

echo current_time( 'Y-m-d H:i:s', 0 );
// '2018-10-28 17:03:22'

Now both implementations return the right date (and time) as set in Settings > General > Timezone.

If you can include this change in your PR, I'll merge it right away.

Thanks again for helping out!

rilwis commented 5 years ago

@cabrerahector You're right. I changed it from true to false, which is now correct :)