Set time Zone by Country

Set time Zone by Country
489 Views
0
(0)

Include the following code file to your current themes functions.php file.

Users can see all time by their country timezone wise.

<?php 
class CXC_change_timezone {

	/**
	 * Class constructor
	 */
	public function __construct() {

		// add callback filter get_comment_date
		add_filter( 'get_comment_date', array( __CLASS__ , 'cxc_change_comment_date_format' ), 10, 3 );

		// add callback filter get_comment_time
		add_filter( 'get_comment_time',  array(  __CLASS__ , 'cxc_get_comment_time' ), 10, 5 );
	}

	/**
    * Return user country timezone date time.
    */
	public function cxc_get_date_time_by_user_timezone( $date ){
		$PublicIP = $this->cxc_get_client_ip();
		$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
		$json = json_decode($json, true);

		if( !empty( $json['timezone'] ) ) {
			$date = date_create($date, timezone_open('America/New_York'));
			date_timezone_set($date, timezone_open($json['timezone']));
			$date = date_format($date, 'Y-m-d g:i a');
		}

		return $date;
	}

	/**
    * Return client country IP.
    */
	public function cxc_get_client_ip() {
		$ipaddress = '';
		if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
			$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
		} else if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
			$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else if ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
			$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
		} else if ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
			$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
		} else if ( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
			$ipaddress = $_SERVER['HTTP_FORWARDED'];
		} else if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
			$ipaddress = $_SERVER['REMOTE_ADDR'];
		} else {
			$ipaddress = 'UNKNOWN';
		}

		return $ipaddress;
	}

	/**
     * Filters the returned comment date.
     *
     * @since 1.5.0
     *
     * @param string|int $date    Formatted date string or Unix timestamp.
     * @param string     $format  PHP date format.
     * @param CXC_Comment $comment The comment object.
     */
	public function cxc_wpsites_change_comment_date_format( $date, $format, $comment ) {

		$date = $this->cxc_get_date_time_by_user_timezone( $comment->comment_date );
		$_format = ! empty( $format ) ? $format : get_option( 'date_format' );
		$date = mysql2date( $_format, $date );

		return $date;
	}

	/**
     * Filters the returned comment time.
     *
     * @since 1.5.0
     *
     * @param string|int $date      The comment time, formatted as a date string or Unix timestamp.
     * @param string     $format    PHP date format.
     * @param bool       $gmt       Whether the GMT date is in use.
     * @param bool       $translate Whether the time is translated.
     * @param CXC_Comment $comment   The comment object.
     */
	public  function cxc_get_comment_time( $date, $format, $gmt, $translate, $comment ){

		$_format = ! empty( $format ) ? $format : get_option( 'time_format' );
		$comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
		$comment_date = $this->cxc_get_date_time_by_user_timezone( $comment_date );
		$date = mysql2date( $_format, $comment_date, $translate );

		return $date;
	}
}

$CXC_change_timezone = new CXC_change_timezone();
?>

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *