在这里,我试图检索用户的IP地址,使用从IP地址获得的时区,我想检查该国的日期是否为1/01/2023,如果是,我想将用户重定向到另一个具有新年快乐页面的子域:
<?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代码
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显示那个国家的倒计时。我在网上找不到这个,所以如果可以的话,请帮忙?谢谢!
发布于 2022-12-04 11:17:05
<?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,并相应地重定向用户。
发布于 2022-12-04 11:18:47
若要根据用户的时区显示2023年元旦倒计时,您需要执行以下操作:
使用$_SERVER'REMOTE_ADDR'.
值得注意的是,您所描述的方法(即根据当前日期和时间将用户重定向到不同的子域)可能不是最方便用户显示倒计时的方法。更好的方法可能是使用JavaScript更新同一页面上的倒计时,而不需要用户导航到不同的URL。这将允许用户看到倒计时,而不必刷新页面或浏览当前正在查看的内容。
下面是一个示例,说明如何根据用户的时区使用PHP和JavaScript实现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);
// 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方法:
<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 thehttps://stackoverflow.com/questions/74675009
复制相似问题