PHP中时间段内的日期验证
如何使用时间戳格式(exp.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");
2014-10-02 13:31:53
)检查日期字符串是否在PHP中的特定时间段内
例如:
$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