简体   繁体   English

我的 PHP 日期时间代码有什么问题?

[英]What's wrong with my PHP datetime code?

I'm not experienced with PHP.我对 PHP 没有经验。 I'm loving it so far, except that this bit of code is working sometimes and other times not at all.到目前为止,我很喜欢它,只是这段代码有时有效,有时则根本不起作用。

I'm trying to redirect based on the time of day.我正在尝试根据一天中的时间重定向。 I have this script set up on multiple domains, with different times for each.我在多个域上设置了这个脚本,每个域都有不同的时间。 Sometimes it works correctly, and sometimes it does not.有时它可以正常工作,有时却不能。 I have modifed my php.ini file as well as .htaccess to try and set the timezone.我修改了我的 php.ini 文件以及 .htaccess 来尝试设置时区。 I have tried writing the time in CST which my server is in(Hostgator), but no luck there either.我曾尝试在我的服务器所在的 CST 中写入时间(Hostgator),但也没有运气。 I should note that there is some HTML that follows my code here, but it's outside of the php.我应该注意到这里有一些 HTML 跟随我的代码,但它在 php.ini 之外。

It doesn't make sense to me why it only works SOMETIMES.为什么它只在某些时候有效,对我来说没有意义。 I've tried clearing my browser cache many times but it never helped.我曾多次尝试清除浏览器缓存,但始终没有帮助。 In this code, it should only display the html below it between 5:47 PM and 6:43 PM and redirect at all other times, right?在这段代码中,它应该只在下午 5 点 47 分到下午 6 点 43 分之间显示它下面的 html 并在所有其他时间重定向,对吗? Is it some sort of caching issue?这是某种缓存问题吗? Is the script being stored and then not being executed when I visit multiple sites in a row?当我连续访问多个站点时,脚本是否被存储然后不被执行? Any help is greatly appreciated.任何帮助是极大的赞赏。

The best case scenario is this is only happening to me because I am visiting the sites too much to make sure the script is working correctly.最好的情况是这只会发生在我身上,因为我访问网站的次数过多,无法确保脚本正常工作。 Is that possible?那可能吗?

<?php
date_default_timezone_set('America/Los_Angeles');
$hour = date("G");
$min = date("i");

if(($hour >= "0" and $min >= "00") and ($hour <="17" and $min <= "47"))
    {
    header('Location: http://mysite.php');
    exit();
}
elseif(($hour >= "18" and $min >= "43") and ($hour <="23" and $min <= "59"))
    {
    header('Location: http://mysite.php');
    exit();
}
?> 

Here is the shorten code, I converted the time to Military time.这是缩短的代码,我将时间转换为军事时间。 If the military time is less than 1747 OR greater than 1843 - it will redirect, else will display the HTML content如果军事时间小于 1747 或大于 1843 - 它将重定向,否则将显示 HTML 内容

            date_default_timezone_set('America/Toronto');
            $hour = date("G");
            $min = date("i");
            $milTime = $hour . $min ;

            if($milTime < 1747 || $milTime > 1843)
            {
            header('Location: http://stackoverflow.com/questions/');
            echo "Redirect";
            exit();
            }
            echo "<h1>$milTime Show HTML Content</h1>";

Try using intval() to get an actual integer and compare that:尝试使用intval()获取实际整数并进行比较:

date_default_timezone_set('America/Los_Angeles');
$hour = intval( date("G"), 10 );
$min = intval( date("i"), 10 );

if(( $hour >= 0 and $min >= 0 ) and ( $hour <= 17 and $min <= 47 ))
{
header('Location: http://mysite.php');
exit();
}
elseif (( $hour >= 18 and $min >= 43 ) and ( $hour <= 23 and $min <= 59 ))
{
header('Location: http://mysite.php');
exit();
}

What you're doing is comparing strings , which are a character-by-character comparison (their order in the ASCII table).您正在做的是比较strings ,这是逐个字符的比较(它们在 ASCII 表中的顺序)。 So, for example, if you compare "13" > "10" , that's correct because "1" == "1" (equals, keeps going) and then "3" > "1" (true, returns true).因此,例如,如果您比较"13" > "10" ,那是正确的,因为"1" == "1" (等于,继续)然后是"3" > "1" (真,返回真)。

But what if you compare "098" > "100" ?但是如果你比较"098" > "100"呢? "0" > "1" (false, returns false). "0" > "1" (假,返回假)。 Obviously it doesn't work for your purposes, since 098 = 98 is actually less than 100 .显然它不适合您的目的,因为 098 = 98实际上小于 100

EDIT: As @DCoder correctly mentioned, the strings are first converted to numbers (integers) and then compared.编辑:正如@DCoder 正确提到的,字符串首先转换为数字(整数),然后进行比较。 I would highly recommend to make sure to convert them to numbers explicitly and then compare to avoid possible issues when dealing with strings.我强烈建议确保将它们显式转换为数字,然后进行比较以避免在处理字符串时可能出现的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM