【问题标题】:How to implement greater than or less than dates in php如何在php中实现大于或小于日期
【发布时间】:2018-03-19 08:00:30
【问题描述】:

我在这里有点困惑。在我的项目中,我需要按日期显示房间的价格。例如 。 从 2017 年 10 月 1 日到 2017 年 10 月 31 日,价格为 200 美元。 从 11 月 1 日到 11 月 30 日的价格是 250 美元,依此类推......

现在我正在尝试这样做......

     <?php 

            $currnt_time = time();
            $from_date = strtotime(2017-10-01);
            $till_date = strtotime(2017-10-31);

            if ($currnt_time >= $from_date && $currnt_time < $till_date){
                $price = $200;
            }

            ?>
          <P> Starts FROM <?php echo $price; ?> INR</P>

但是当我运行这个脚本时,它说 undefined variable $price 。我不明白我错在哪里),因为我将有很多像上面这样的日期时段,所以解决这个问题的最佳方法是什么。 请帮忙。谢谢!

【问题讨论】:

  • 除了一些我认为来自 sn-p 编辑的语法错误之外,should work

标签: php date time timestamp


【解决方案1】:

$price 的值前面有一个 $,并且您已经忘记在日期字符串之前和之后添加引号。

<?php 
    $currnt_time = time();
    $from_date = strtotime('2017-10-01');
    $till_date = strtotime('2017-10-31');

    if ($currnt_time >= $from_date && $currnt_time < $till_date){
        $price = 200;
    }

    ?>
  <P> Starts FROM <?php echo $price; ?> INR</P>
?>

【讨论】:

  • 谢谢!工作......小混乱......哪个日期总是更大的过去或未来?
  • 还有一个小问题,我怎样才能动态放置年份而不是像 2017 那样硬编码 ....???因为在上述情况下,我需要每年更改我的代码.. 请提供任何解决方案
  • 使用 php 的日期。日期('Y');
  • 但是如果我有从 2017 年 12 月 2 日到 2018 年 1 月 2 日这样的时段呢?
  • 无论您拥有什么,strtotime() 都会将其转换为正确的值。未来比过去更有价值。 @kohali
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多