首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取国家使用ip地址和输出新年倒计时根据

获取国家使用ip地址和输出新年倒计时根据
EN

Stack Overflow用户
提问于 2022-12-04 11:13:21
回答 2查看 27关注 0票数 0

在这里,我试图检索用户的IP地址,使用从IP地址获得的时区,我想检查该国的日期是否为1/01/2023,如果是,我想将用户重定向到另一个具有新年快乐页面的子域:

代码语言:javascript
复制
<?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');
}*/
?>

我的倒计时scripts.js代码

代码语言:javascript
复制
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);

我正在努力实现timeanddate.com在这个链接https://www.timeanddate.com/countdown/newyear上显示的内容。

如何使用IP显示那个国家的倒计时。我在网上找不到这个,所以如果可以的话,请帮忙?谢谢!

EN

回答 2

Stack Overflow用户

发布于 2022-12-04 11:17:05

代码语言:javascript
复制
<?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');
}
?>

使用DateTime类和DateTimeZone类检索用户IP地址时区中的日期和时间。然后,检查日期,看看该时区是否为1/01/2023。代码检查用户时区中是否为11/01,并相应地重定向用户。

票数 0
EN

Stack Overflow用户

发布于 2022-12-04 11:18:47

若要根据用户的时区显示2023年元旦倒计时,您需要执行以下操作:

使用$_SERVER'REMOTE_ADDR'.

  • Use (类似于http://ip-api.com的服务)检索用户的IP地址,以便根据用户的IP地址确定用户的时区。您可以通过向以下URL发出HTTP请求来做到这一点:http://ip-api.com/json/{IP_ADDRESS},其中{IP_ADDRESS}是用户的IP地址。这将返回一个JSON对象,其中包含有关用户位置的信息,包括他们的时区。

  • 使用时区信息在用户的时区中创建一个新的日期对象,表示2023年元旦。您可以使用日期构造函数并传入一个格式为"Jan 1,2023 00: 00:00:00“的字符串,然后使用getTime()方法在用户的time‘s time zone.

  • Calculate中获取该日期的时间戳--直到2023年元旦的剩余时间,方法是从用户时区的2023年新年时间戳中减去当前时间戳。

  • 将剩余时间除以一天、小时、分钟和秒中的毫秒数,以确定日、小时、分钟的数量,直到2023年元旦。

  • 使用这些信息更新页面上适当的HTML元素的文本内容以显示倒计时。我建议检查日期构造函数和getTime()方法的文档,以确保正确使用它们。您还应该确保PHP和JavaScript代码的格式正确,并且没有语法错误。

值得注意的是,您所描述的方法(即根据当前日期和时间将用户重定向到不同的子域)可能不是最方便用户显示倒计时的方法。更好的方法可能是使用JavaScript更新同一页面上的倒计时,而不需要用户导航到不同的URL。这将允许用户看到倒计时,而不必刷新页面或浏览当前正在查看的内容。

下面是一个示例,说明如何根据用户的时区使用PHP和JavaScript实现2023年元旦倒计时:

代码语言:javascript
复制
 <?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);

// Use this information to update the text content of the appropriate HTML
// elements on your page to display the countdown
echo "<div id='countdown'>";
echo "<span id='days'>$days</span> days, ";
echo "<span id='hours'>$hours</span> hours, ";
echo "<span id='minutes'>$minutes</span> minutes, and ";
echo "<span id='seconds'>$seconds</span> seconds";
echo "</div>";

// 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/');
}
?>

此代码使用PHP检索用户的IP地址,确定他们的时区,并计算到2023年元旦之前的剩余时间。然后,它使用这些信息更新页面上适当HTML元素的文本内容,可以使用CSS来显示倒计时。

然后可以使用JavaScript每秒钟更新一次倒计时,使用setInterval方法:

代码语言:javascript
复制
  <script>
// Get references to the HTML elements that will display the countdown
const days = document.getElementById('days');
const hours = document.getElementById('hours');
const minutes = document.getElementById('minutes');
const seconds = document.getElementById('seconds');

// Define a function that calculates the time remaining until New Year's Day 2023
// in the user's time zone and updates the text content of the
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74675009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档