doutong1890 2013-09-11 10:19
浏览 28
已采纳

比较两个数据库条目并生成span类

I wonder if anyone could help me with this.

I have two fields in a database that are shown using:

1-<?php echo JHTML::_('date', $row->created_date, 'd/m/Y'); ?>

2-<?php echo $row->delivery_date; ?>

The first one, is written to the database as a full date with time, and as you can see above, I strip the output to only show the date. In the database it would appear as '2013-09-10 11:56:52'

The second is from a text entry form field, that has just text saved to the database in the format d/m/Y. In the database this appears as '19/09/2013'

Is there a way I can produce an if statement, that will add a span and class tag around the 2nd line if this condition is true:

"If the $row->delivery_date is within 21 days after (and including) $row->created_date."

Would it ultimately be best if I made them both full date values? Would that make it easier to calculate how many days apart they are?

  • 写回答

1条回答 默认 最新

  • dtbsezxw28056 2013-09-12 00:58
    关注
    1. Convert Dates to a common format using strtotime() (e.g. PHP works nicely with unix timestamp's):

      $created_date = strtotime($row->created_date); // These are basically seconds $deliver_date = strtotime($row->delivery_date);

    2. Calculate the difference between the two and covert it to days (each day is 86400 seconds):

      $days = ceil(abs($deliver_date - $created_date) / 86400);

    3. Check $days in your if() statement for echoing your span.

    Something like:

    if($days <= 21)
    {
        echo '<span class="delivery_warning">' . $row->delivery_date . '</span>';
    }
    else
    {
        else { echo $row->delivery_date; };
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?