2012-04-26 34 views
-1

可能重复:
How to check if a date is in a given range?
How to check if date(entered by user) is in given range (Date format :-day month ie.:-1 june)查找时间是否在规定的范围内

我试图找到一个日期是否在规定范围内。我正在使用以下代码:

$apple='25 March'; 
    $udate= date('d F',strtotime($apple)); 

    echo $udate; 
    $startDate='21 March'; 
    $realStartDate= date('d F',strtotime($startDate)) ; 
    echo $realStartDate; 
    $endDate='19 April'; 
    $realEndDate= date('d F',strtotime($endDate)) ; 
    if ($udate >= $realStartDate && $udate <= $realEndDate) { 
     echo 'within tange'; 
    } 
    else{ 
     echo 'Not in range'; 
    } 
    ?> 

我在哪里出错了?

+0

你有没有做你的责任和搜查? http://stackoverflow.com/questions/976669/how-to-check-if-a-date-is-in-a-given-range – 2012-04-26 07:43:14

+0

你研究过mysql之间还是这个链接:http:// www。 daniweb.com/web-development/databases/mysql/threads/53025/mysql-select-rows-in-a-date-range – SuperNoob 2012-04-26 07:58:58

+0

为什么要将字符串转换为时间戳然后回到完全相同的字符串? – JJJ 2012-04-26 09:46:44

回答

0

喜欢这个

if(strtotime($givendate) > strtotime('3/21/xxxx') && strtotime($givendata) < strtotime('4/19/xxxx')) { 
    // Its within range 
} 
0

您可以使用DateTime

$userDate = new DateTime("2012-03-01"); 

if ($userDate > new DateTime("2012-03-21 00:00:00") && $userDate < new DateTime("2012-04-19 23:59:59")) 
{ 
    // In Range 
} 

把它在一个函数,如果格式是(7月1日)

if (inRange ("1 June", "3 March", "7 December")) { 
    echo "In Range"; 
} else { 
    echo "Out Of Range"; 
} 

function inRange($dateCheck, $dateFrom, $dateTo) { 

    $date = DateTime::createFromFormat ("d F", $dateCheck); 
    $date1 = DateTime::createFromFormat ("d F", $dateFrom); 
    $date2 = DateTime::createFromFormat ("d F", $dateTo); 

    if ($date > $date1 && $date < $date2) { 
     return true; 
    } 

    return false; 

} 
+0

它在格式天的月份,即:(7月1日) – spsingh 2012-04-26 08:01:03

+0

刚刚更新我的回答 – Baba 2012-04-26 08:14:02

1

比较时间戳不是字符串交涉!

if(strtotime($apple) < strtotime($endDate) && strtotime($apple) > strtotime($startDate)){ 
// All ok! 
} 
0

试试这个

if (strtotime($udate) >= strtotime($realStartDate) && strtotime($udate) <= strtotime($realEndDate)) { 
    echo 'within tange'; 
} 
else{ 
    echo 'Not in range'; 
} 
2

试试这个它的工作......

<?php 
     $udate   = '25 March'; 
     $udateTimestamp = strtotime($udate); 


     $startDate   = '21 March'; 
     $startDateTimestamp = strtotime($startDate); 

     $endDate   = '19 April'; 
     $eEndDateTimestamp = strtotime($endDate); 

     if ($udateTimestamp >= $startDateTimestamp && $udateTimestamp <= $eEndDateTimestamp) 
     { 
       echo 'within tange'; 
     } 
     else 
     { 
       echo 'Not in range'; 
     } 
?> 
相关问题