Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Country using ip address and output New Year Countdown according to that

Here I am trying to retrieve the IP address of the user, and with the timezone that I got from the IP address, I want to check if the date is 1/01/2023 for that country and then if it is, I want to redirect the user to another subdomain which has a Happy New Year Page:

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ipInfo = file_get_contents('http://ip-api.com/json/' . $ip);
$ipInfo = json_decode($ipInfo);
$timezone = $ipInfo->timezone;
if (date('d') == '01' && date('m') == '01' && date('Y') == '2023') {
  header('Location: https://2023.blogsaffair.com/');
}

if (date('d') == '1' && date('m') == '11') {
  header('Location: https://blogsaffair.com');
}*/
?>

My scripts.js code for countdown

const secondsText = document.getElementById('seconds');
const minutesText = document.getElementById('minutes');
const hoursText = document.getElementById('hours');
const daysText = 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));

    secondsText.textContent = secondsValue;
    minutesText.textContent = minutesValue;
    hoursText.textContent = hoursValue;
    daysText.textContent = daysValue;

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

setInterval(countDown, 1000);

I am trying to achieve what timeanddate.com shows on this link https://www.timeanddate.com/countdown/newyear

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I display the countdown for that country using IP. I couldn’t find this anywhere on the internet so please help if you can? Thanks!

>Solution :

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ipInfo = file_get_contents('http://ip-api.com/json/' . $ip);
$ipInfo = json_decode($ipInfo);
$timezone = $ipInfo->timezone;

// Get the current date and time in the timezone of the user's IP address
$date = new DateTime('now', new DateTimeZone($timezone));

// Check if the date is 1/01/2023 in the timezone of the user's IP address
if ($date->format('d') == '01' && $date->format('m') == '01' && $date->format('Y') == '2023') {
  header('Location: https://2023.blogsaffair.com/');
}

// If not, check if it's 11/01 in the user's timezone
if ($date->format('d') == '1' && $date->format('m') == '11') {
  header('Location: https://blogsaffair.com');
}
?>

the date and time in the timezone of the user’s IP address is retrieved using the DateTime class and the DateTimeZone class. Then, the date is checked to see if it is 1/01/2023 in that timezone. the code checks if it is 11/01 in the user’s timezone and redirects the user accordingly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading