Unable to get User’s timezoneoffset from js to PHP
-
Hi,
I am trying to convert a set of online classes with CDT time to times based on user’s timezone.
As part of this, I am trying to get user’s timezoneoffset from Javascript to PHP Shortcode handler using the following codes:
my-custom-hooks.php
<?php /* Plugin Name: JKY-Custom-Hooks Version : 1.0 Description: Demonstrating WordPress Hooks (Actions and Filters) with multiple examples. Author : Sam Author URI : https://staging59.courses.jkyog.org/ License : GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: my-custom-hooks */ /** * Proper way to enqueue scripts and styles. */ function wpdocs_load_js_scripts() { wp_enqueue_script( 'jquery'); wp_enqueue_script( 'moment-js', plugins_url('/assets/js/moment-js/moment.min.js', __FILE__ ), array(), '1.0.0', false); wp_enqueue_script( 'moment-timezone', plugins_url('/assets/js/moment-js/moment-timezone.min.js', __FILE__ ), array(), '1.0.0', false); wp_enqueue_script( 'moment-timezone-data', plugins_url('/assets/js/moment-js/moment-timezone-with-data.min.js', __FILE__ ), array(), '1.0.0', false); wp_enqueue_script( 'moment-timezone-tenyears', plugins_url('/assets/js/moment-js/moment-timezone-with-data-10-year-range.min.js', __FILE__ ), array(), '1.0.0', false); wp_enqueue_script( 'moment-timezone-nineteenseventy', plugins_url('/assets/js/moment-js/moment-timezone-with-data-1970-2030.min.js', __FILE__ ), array(), '1.0.0', false); wp_enqueue_script('custom-date-scripts', plugins_url( '/assets/js/custom-date-scripts.js' , __FILE__ )); } add_action( 'wp_enqueue_scripts', 'wpdocs_load_js_scripts' ); function wpb_hook_process_classes(){ $online_classes = array ( "Daily Satsang" => array ( "time_val" => "7:00 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Kripalu Paddhati" => array( "time_val" => "8:00 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "7 Divine Laws - Book Club" => array( "time_val" => "7:00 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Bhagavad Gita Study" => array( "time_val" => "7:00 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Prem Yoga" => array( "time_val" => "9:30 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Upanishad Sarita" => array( "time_val" => "9:30 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Weekly Satsang" => array( "time_val" => "10:30 AM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Bhagavad Gita Recitation" => array( "time_val" => "6:00 PM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Daily Scripture Reading - Ramayan" => array( "time_val" => "7:00 PM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Daily Meditation" => array( "time_val" =>"7:30 PM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ), "Nutrition and Lifestyle Coaching" => array( "time_val" => "8:00 PM", "time_zone" => "CDT", "zoom_link" => "https://us02web.zoom.us/j/83469428800", "attendance_tracker" => "" ) ); // Get the user's timezone offset from the AJAX request data $userTimezoneOffset = $_POST['timezoneOffset']; // Convert the times to the user's timezone $online_classes_usertimes = convertTimesToUserTimezone($online_classes, $userTimezoneOffset); // Return the list of times in the user's timezone as a JSON response $response = array('online_classes_usertimes : hello : ' => $online_classes_usertimes); echo json_encode($response); } add_shortcode( "process_online_classes", "wpb_hook_process_classes" ); // Function to convert a list of online classes with times from one timezone to another function convertTimesToUserTimezone($online_classes, $userTimezoneOffset) { echo "userTimezoneOffset : " . $userTimezoneOffset; // Calculate the user's timezone offset in hours $userTimezoneOffsetHours = $userTimezoneOffset / 60; // Create a DateTimeZone object for the user's timezone $userTimezone = new DateTimeZone(sprintf('%+d', $userTimezoneOffsetHours)); // Create a DateTimeZone object for the original timezone (CDT in this example) $originalTimezone = new DateTimeZone('America/Chicago'); // Convert each time to the user's timezone $online_classes_usertimes = array(); foreach ($online_classes as $online_class) { $dateTime = new DateTime($online_class["time_val"], $originalTimezone); $dateTime->setTimezone($userTimezone); $online_classes_usertimes[] = $dateTime->format('Y-m-d h:i:s A'); } // Return the list of online classes in the user's timezone return $online_classes_usertimes; } //================================================= // Security: Abort if this file is called directly //================================================= if ( !defined('ABSPATH') ) { die; } if (isset($_POST['timezoneOffset'])) { wpb_hook_process_classes(); } ?>custom-date-scripts.js
var jq = jQuery.noConflict(); // Call the function when the page loads jq(document).ready(function() { getUserTimezoneOffset(); }); // Function to get the user's timezone offset and send it to the server via AJAX function getUserTimezoneOffset() { var userTimezoneOffset = new Date().getTimezoneOffset(); debugger; try { jQuery.ajax({ url: '../../my-custom-hooks.php', type: 'POST', data: { 'timezoneOffset': userTimezoneOffset }, noConflict: true, success: function(response) { // Handle the server response here jQuery('#result').html(response); }, error: function(xhr, status, error) { // handle the error here console.log("Error: " + error); } }); } catch(error) { // handle the error here console.log("Error: " + error); } }On the front-end, I am using a wordpress Shortcode widget [process_online_classes]
But I am unable to get the user’s timezoneoffset from Javascript to PHP. And hence all the times are converted to UTC as default, instead of the IST (currently set system timezone.)
Someone please help in this regard.The page I need help with: [log in to see the link]
The topic ‘Unable to get User’s timezoneoffset from js to PHP’ is closed to new replies.