[英]How to check if a date is between other dates?
我从GET得到两个日期
$start = $_GET['start'];
$end = $_GET['end'];
然后,我将它们转换为日期:
$start = \DateTime::createFromFormat('d-m-yy', $start);
$end = \DateTime::createFromFormat('d-m-yy', $end);
如果我var_dump他们我得到
object(DateTime)#8260 (3) { ["date"]=> string(26) "2019-02-01 18:05:42.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
object(DateTime)#7967 (3) { ["date"]=> string(26) "2019-02-05 18:05:42.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
然后,我循环发布帖子,并在自定义字段中有一个日期,它是一个字符串,因此我将其转换为日期:
foreach($posts as $post) {
$myDate = \DateTime::createFromFormat('d-m-yy', usp_get_meta(false,'usp-custom-80'));
如果我vur_dump我得到了
object(DateTime)#8457 (3) { ["date"]=> string(26) "2000-03-20 18:07:28.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
然后我终于做到了:
if ( ( $myDate >= $start) && ( $myDate <= $end) ) {
....
但我无法获得正确的结果,好像该条件根本不起作用
您可以尝试:
if ( ( $myDate->getTimestamp() >= $start->getTimestamp()) && ( $myDate->getTimestamp() <= $end->getTimestamp()) )
您可以比较从Unix纪元开始的秒数。
if ( ($start->format('U') <= $myDate->format('U'))
&& ( $myDate->format('U') <= $end->format('U'))) {
// date is between start and end
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.