简体   繁体   English

需要帮助才能理解php函数

[英]need help to understand a php function

Need help understanding why this doesn't work. 需要帮助理解为什么这不起作用。 I have a text that's only visible on a given start date and time and gets removed on ended date and time. 我有一个只在给定的开始日期和时间可见的文本,并在结束的日期和时间被删除。

This is the code I am using that works just fine. 这是我正在使用的代码,工作得很好。 The text is visible for a month without problems: 文本可见一个月没有问题:

<?php 
     if ( date('Y/m/d H:i') >= "2017/04/06 08:00" && 
          date('Y/m/d H:i') <= "2017/05/06 20:00" ) { 
         echo "$text_1"; 
     } else { 
         echo "$text_2"; 
     }
?>

This is how I would like to be able write the date but it doesn't work as it supposed to: 这就是我希望能够写日期的方式,但它不能正常工作:

<?php 
     if ( date('d/m/Y H:i') >= "06/04/2017 08:00" && 
          date('d/m/Y H:i') <= "06/05/2017 20:00" ) { 
         echo "$text_1"; 
     } else { 
         echo "$text_2"; 
     }
?>

It doesn't take into account the month or year. 它没有考虑月份或年份。 Why is that? 这是为什么? This would only show the text for one day and not for a month. 这只会显示一天的文本而不是一个月的文本。

Change date into timestamp and then compare. 将日期更改为时间戳然后进行比较。 Format will not matter 格式无关紧要

if ( strtotime(date("Y-m-d H:i")) >= strtotime("06/04/2017 08:00") && 
    strtotime(date("Y-m-d H:i")) <= strtotime("06/05/2017 20:00" )) { 
    echo "$text_1"; 
} 
else { 
    echo "$text_2"; 
}

Its does not matter the formate now whether "Ymd H:i" or d/m/YH:i . 现在,无论是"Ymd H:i"还是d/m/YH:i都无关紧要d/m/YH:i You can do any formate. 你可以做任何甲酸盐。

You can also do as follows 您还可以执行以下操作

if ( strtotime(date("d/m/Y H:i")) >= strtotime("06/04/2017 08:00") && 
    strtotime(date("d/m/Y H:i")) <= strtotime("06/05/2017 20:00" )) { 
    echo "aaa"; 
} 
else { 
    echo "bbb"; 
}

The date format really does not matter, when ever you try to compare dates, Convert that in seconds and then compare . 日期格式无关紧要,当您尝试比较日期时,将其转换为秒,然后进行比较。
Just take a lot at strtotime() method and do examples and see. 只需要花费很多strtotime()方法并做例子,看看。

The problem is that you compare strings using >= and <= which is not possible in most cases. 问题是您使用>=<=来比较字符串,这在大多数情况下是不可能的。

Here's what PHP does when executing the comparison date('Y/m/d H:i') >= "2017/04/06 08:00" : 以下是PHP在执行比较date('Y/m/d H:i') >= "2017/04/06 08:00"时所执行的操作date('Y/m/d H:i') >= "2017/04/06 08:00"

First, the date() function is executed, which returns a string like "2017/04/23 12:51" 首先,执行date()函数,返回类似"2017/04/23 12:51"的字符串

Since >= can only compare numbers, both strings are then implicitly casted into double . 由于>=只能比较数字,因此两个字符串都被隐式地转换为double For this, PHP uses the strtod function. 为此,PHP使用strtod函数。 This function parses a string from left to right. 此函数从左到右解析字符串。 If the first thing it finds (except for whitespace) is a number/ infinity / NAN , this number is returned. 如果它找到的第一件事(除了空格)是数字/ infinity / NAN ,则返回此数字。 As soon as strtod finds a non-numeric character, the rest of the string is ignored. 一旦strtod找到非数字字符,字符串的其余部分就会被忽略。 If strtod does not find a number, it returns 0 . 如果strtod找不到数字,则返回0

In this case, strtod returns only the year because it does not understand what a / means, and therefore ignores everything after the year. 在这种情况下, strtod只返回年份,因为它不了解a /含义,因此忽略了一年后的所有内容。


If you want to compare dates properly, you have to use timestamps . 如果要正确比较日期,则必须使用时间戳 To get the current timestamp, PHP offers the time() function. 要获取当前时间戳,PHP提供了time()函数。


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

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