doushizhou4477 2017-10-10 10:01
浏览 28

如何在php中验证内容的开始和结束日期时间

I am working on a news portal in which a news (content) will expire after a specific time.The problem is i have to show only one news at one time, i am not sure how to validate it and check that if there is already a news exist of the same duration in database?

Note user can add future (upcoming) news too. Example is below

$newsStartDate = '2017-10-10 14:52:10'; 
$newsEndDate = '2017-10-11 14:53:10';

I have 2 datetime type column in database (start,expire) now i have to that weather there is already any news exist in the duration of selected $newsStartDate,$newsEndDate or not.

  • 写回答

1条回答 默认 最新

  • duandaodao6951 2017-10-10 10:10
    关注

    It's better to make this comparison in database with BETWEEN in your SELECT from database. Example:

    SELECT * FROM News WHERE datetime BETWEEN 2017-10-10 14:52:10 AND 2017-10-11 14:53:10;
    

    Or in your case:

    SELECT * FROM News WHERE start < NOW() AND expire > NOW();
    

    But if you want to compare dates in php you could use comparison operators to deal with dates:

    if ($newsDate > $newsStartDate && $newsDate < $newsEndDate) { }
    

    See more here:

    PHP check if date between two dates

    评论

报告相同问题?

悬赏问题

  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改