检查时间戳之间是否存在日期


Check if day exists between timestamps

我正试图弄清楚两个unix时间戳之间是否存在日期,而不考虑年份。例如,假设我有12月12日的日期,但不包含年份。我该如何检查12月12日是否存在于1353369600和1358640000的时间戳之间(第一个时间戳等于2012年11月20日;第二个等于2013年1月20日)。我正在用PHP编写应用程序,但是如果你知道如何用不同的语言来完成这项工作,请发表你的想法,这样我就可以尝试完成逻辑了。

提前感谢

更新:答案在这里!使用strtotime并将第二个参数设置为开始时间戳:)

只需使用strtotime将字符串转换为时间戳,然后进行比较。如果字符串不包含年份,则年份默认为当前年份。

$ts = strtotime("December 12th");
if ($ts >= 1353369600 && $ts <= 1358640000 ) {//....}

您可以在每年的时间戳之间进行检查,看看它们之间是否有所需的日期。

function inBetween($day, $month, $from, $to)
{
    $from_year = date('Y', $from);
    $to_year = date('Y', $to);
    if($from_year == $to_year)
    {
        $time = mktime(12,0,0,$month,$day, $from_year);
        return $time > $from && $time < $to;
    }
    elseif($from_year < $to_year)
    {
        for($i=$from_year;$i<=$to_year;$i++)
        {
            $time = mktime(12,0,0,$month,$day, $i);
            if($time > $from && $time < $to) return TRUE;
        }
        return FALSE;
    }
}
var_dump(inBetween(12, 12, 1353369600, 1358640000));