0

index_test.php file which retrieves the user's IP address, determine their time zone, and calculates the time remaining until New Year's Day 2023

<?php
    // Get the user's IP address
    $ip = $_SERVER['REMOTE_ADDR'];
    
    // Use a service like http://ip-api.com to determine the user's time zone
    // based on their IP address
    $ipInfo = file_get_contents('http://ip-api.com/json/' . $ip);
    $ipInfo = json_decode($ipInfo);
    $timezone = $ipInfo->timezone;
    
    // Use the time zone information to create a new Date object representing
    // New Year's Day 2023 in the user's time zone
    $deadlineDate = new Date("Jan 1, 2023 00:00:00", $timezone);
    $deadlineTimestamp = $deadlineDate->getTime();
    
    // Calculate the time remaining until New Year's Day 2023
    $presentDate = new Date();
    $presentTimestamp = $presentDate->getTime();
    $timeLeft = $deadlineTimestamp - $presentTimestamp;
    
    // Divide the time remaining by the number of milliseconds in a day, hour,
    // minute, and second to determine the number of days, hours, minutes, and
    // seconds remaining until New Year's Day 2023
    $days = floor($timeLeft / (1000 * 60 * 60 * 24));
    $hours = floor(($timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    $minutes = floor(($timeLeft % (1000 * 60 * 60)) / (1000 * 60));
    $seconds = floor(($timeLeft % (1000 * 60)) / 1000);
    
    // If the current date and time are New Year's Day 2023 in the user's time zone,
    // redirect the user to a different URL
    if ($presentTimestamp >= $deadlineTimestamp) {
        header('Location: https://2023.blogsaffair.com/');
    }
    ?>

scripts.js file

const seconds = document.getElementById('seconds');
const minutes = document.getElementById('minutes');
const hours = document.getElementById('hours');
const days = document.getElementById('days');

function countDown () {
    var deadlineDate = new Date("Jan 1, 2023 00:00:00").getTime();
    var presentDate= new Date().getTime();
    var timeLeft = deadlineDate - presentDate;
    var daysValue = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
    var hoursValue = Math.floor(timeLeft % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
    var minutesValue = Math.floor(timeLeft % (1000 * 60 * 60) / (1000 * 60));
    var secondsValue = Math.floor(timeLeft % (1000 * 60) / (1000));

    seconds.textContent = secondsValue;
    minutes.textContent = minutesValue;
    hours.textContent = hoursValue;
    days.textContent = daysValue;

    if(timeLeft < 0){
        clearInterval();
    }   
}

setInterval(countDown, 1000);

My HTML Code

<div class="text-center">
                <h2>COUNTDOWN TO 2023.</h2>
                <div class="time-cards">
                    <div class="time-card">
                        <?php echo "<h4 class='time' id='days'>$days</h4>"; ?>
                        <p class="time-display">Days</p>
                    </div>
                    <div class="time-card">
                       <?php echo  "<h4 class='time' id='hours'>$hours</h4>"; ?>
                        <p class="time-display">Hours</p>
                    </div>
                    <div class="time-card">
                        <?php echo "<h4 class='time' id='minutes'>$minutes</h4>"; ?>
                        <p class="time-display">Minutes</p>
                    </div>
                    <div class="time-card">
                        <?php echo "<h4 class='time' id='seconds'>$seconds</h4>"; ?>
                        <p class="time-display">Seconds</p>
                    </div>
                </div>
            </div>

Now the countdown is not syncing with the IP address country and it outputs my device time zone countdown, rather than IP address country time zone countdown. So I need it to display countdown for my current IP till New Years rather than focus on my PC time zone to display the countdown. I hope you guys can help! Thanks!

  • Check https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript – James Dec 04 '22 at 17:12
  • I checked it shows that I have to set a default time zone in my code but I don't want that, after retrieving country from IP, I want it to use that country to display the countdown on the page. – saurishmonga Dec 04 '22 at 17:19

0 Answers0