2012-06-28 25 views

回答

0

使用

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]]) 

manual page

这样填写参数和减去

time() 

,如果是大于零,它在未来

0
<?php 

$date = "01/06/2012"; 

if(list($dd,$mm,$yyyy) = explode('/',$date)) 
{ 

    if(($timestamp = mktime(0, 0, 0, $mm, $dd, $yyyy)) && $dd && $mm && $yyyy) 
    { 
     if($timestamp>time()) 
     { 
      echo "ok"; 
     } 
     else 
     { 
      echo "Pls enter a time in future"; 
     } 
    } 
    else 
    { 
     echo "format error"; 
    } 

} 
else 
{ 
    echo "format error"; 
} 

?> 
5

可以使用DateTime类做这种比较,以及:

$now  = new DateTime(); 
$user_date = DateTime::createFromFormat('d/m/Y', $userDate); 
if ($user_date >= $now) 
{ 
    echo 'Date is not in the past'; 
} 
+0

日期应该使用时间戳进行比较。请查看我的答案为例。 –

+1

@ keithm为什么只应该将日期作为时间戳进行比较? DateTime对象具有可比性并且更具可读性。 –

+0

我会在我的回答中更好地解释。但是请注意,DateTimeImmutable :: createFromFormat需要将时间字符串作为第二个参数传递,并且您不检查格式,因此以下操作可能会出错,并且代码不会抛出任何内容:用户日期:2015年11月10日。如果有人通过11/10/2015而非10/11/2015,结果将是错误的,没有人会注意到。 –

0

我建议这个解决方案:

/** 
* Is future date 
* 
* Checks if given date is today 
* or in the future. 
* 
* @param string $date 
* 
* 
* @return bool 
* 
*/ 
protected function isFutureDate($date) 
{ 
    // assuming the date is in this format 
    // (I use value objects for these things so that 
    // I don't have to repeat myself) 

    $date = DateTimeImmutable::createFromFormat('d-m-Y', $date); 
    $today = DateTimeImmutable::createFromMutable(new DateTime()); 
    return $date->getTimestamp() >= $today->getTimestamp(); 
} 

我使用由任何其他日期延长级以下的Date对象。其他日期类都会有自己的规则(如日期必须是未来的等),从这个基类扩展:

/** 
* Class DateValueObject 
* 
* 
* 
* @package Hidden\Model\Shared\ValueObject 
* 
*/ 
abstract class DateValueObject extends ValueObject 
    implements DateValueObjectContract 
{ 

    /** 
    * @var DateValueObject 
    * 
    */ 
    protected $value; 


    /** 
    * Initialises the Date. 
    * 
    * @param $date 
    * 
    * @throws InvalidDateException 
    * 
    */ 
    public function __construct($date) 
    { 
     if (is_string($date)) { 
      $this->value = $this->setDate($date); 
     } else { 
      throw new InvalidDateException(
       'String Expected. Got: ' .$date 
      ); 
     } 
    } 


    /** 
    * Set valid date. 
    * 
    * @param string $date 
    * 
    * @return DateTimeImmutable 
    * 
    * @throws InvalidDateException 
    */ 
    protected function setDate($date) 
    { 
     try { 
      $d = DateTimeImmutable::createFromFormat('d-m-Y', $date); 
      if($d && $d->format('d-m-Y') == $date) { 
       return $d; 
      } else { 
       $d = DateTimeImmutable::createFromFormat('d/m/Y', $date); 
       if($d && $d->format('d/m/Y') == $date) { 
        return $d; 
       } 
       throw new InvalidDateException(
        'Date cannot be formatted: ' .$date 
       ); 
      } 
     } catch (\Exception $e) { 
      throw new InvalidDateException(
       'For date: ' .$date .' with message' .$e->getMessage() 
      ); 
     } 
    } 



    /** 
    * Get the date to string 
    * 
    * @param string $format 
    * 
    * @return string 
    * 
    */ 
    public function toString($format = 'd/m/Y') 
    { 
     return $this->value->format($format); 
    } 


    /** 
    * Get the date as immutable object. 
    * 
    * 
    * @return DateTimeImmutable|Date 
    * 
    */ 
    public function toDateObject() 
    { 
     return $this->value; 
    } 
} 

编辑:请注意,代码检查第一个块,如果日期是在未来与否,而第二个例子是有一个可扩展的类,以便其他日期类(生日,发票日期等)可以扩展而无需重复代码。您可以将它放在一个方法中,并在每次需要时复制并粘贴它,或者只是从中扩展它,因为它知道它可以在整个板上运行。

我的例子接受“d-m-Y”和“d/m/Y”并相应地处理格式。在一天结束时,你仍然有一个php DateTimeImmutable对象,但知道如何构建自己。

+1

您断言应该比较日期,因为时间戳不正确。你写了很多代码来做什么DateTime已经做得更好。 –

+0

日期不能作为时间戳进行比较;时间戳不是时区知晓或夏令时识别 –

相关问题