Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中时间段内的日期验证_Php_Date_Timestamp - Fatal编程技术网

PHP中时间段内的日期验证

PHP中时间段内的日期验证,php,date,timestamp,Php,Date,Timestamp,如何使用时间戳格式(exp.2014-10-02 13:31:53)检查日期字符串是否在PHP中的特定时间段内 例如: 每日时段:今天、明天、昨天、两天前,依此类推 周周期:本周、下周、上周、两周前,依此类推 月周期:本月、下月、上月、两个月前,依此类推 年度期间:今年、明年、去年、两年前,依此类推 我建议使用类(PHP5.2+)并使用比较运算符。例如,比较本月的if,如下所示: $start = new DateTime("First day of this month 00:00:00");

如何使用时间戳格式(exp.
2014-10-02 13:31:53
)检查日期字符串是否在PHP中的特定时间段内

例如:

  • 每日时段:今天、明天、昨天、两天前,依此类推
  • 周周期:本周、下周、上周、两周前,依此类推
  • 月周期:本月、下月、上月、两个月前,依此类推
  • 年度期间:今年、明年、去年、两年前,依此类推
  • 我建议使用类(PHP5.2+)并使用比较运算符。例如,比较本月的if,如下所示:

    $start = new DateTime("First day of this month 00:00:00");
    $end = new DateTime("Last day of this month 23:59:59");
    $datetotest = new DateTime("2014-10-02 13:31:53");
    
    if($datetotest >= $start and $datetotest <= $end) {
    // do stuff
    }
    
    $start=new DateTime(“本月第一天00:00:00”);
    $end=新日期时间(“本月最后一天23:59:59”);
    $datetotest=新的日期时间(“2014-10-02 13:31:53”);
    
    如果($datetotest>=$start和$datetotest=$start和$date,我如何知道应该为
    DateTime()
    )使用哪个参数?请看一下。这列出了相对日期的所有有效相对用法,例如“3年前”。否则,您可以使用任何有效的格式化日期,如示例中所示。是的,这正是我要查找的。谢谢
    function isInThisMonth(DateTime $date) {
         $start = new DateTime("First day of this month 00:00:00");
         $end = new DateTime("Last day of this month 23:59:59");
         return ($date >= $start and $date <= $end);
    }
    
    if(isInThisMonth($datetotest)) // do stuff