Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将日期与今天的日期进行比较_Php_Date - Fatal编程技术网

PHP将日期与今天的日期进行比较

PHP将日期与今天的日期进行比较,php,date,Php,Date,我正在尝试以mm yy的格式获取信用卡到期日,查看该日期是否已过,以便知道信用卡是否已过期。如果已过期,则在上插入一类过期的 问题 我的代码检查了一个样本日期05/16,脚本说这张卡没有过期,显然这张卡已经有一年了 我的代码 <?php foreach($card_historys as $card_history){ $expired = ""; $date = strtotime($card_history->expire); //Returns a date

我正在尝试以
mm yy
的格式获取信用卡到期日,查看该日期是否已过,以便知道信用卡是否已过期。如果已过期,则在
上插入一类
过期的

问题 我的代码检查了一个样本日期05/16,脚本说这张卡没有过期,显然这张卡已经有一年了

我的代码

<?php foreach($card_historys as $card_history){ 
    $expired = "";
    $date = strtotime($card_history->expire); //Returns a date in mm/yy
    $noww = strtotime(date('m/y'));

    echo "expire: ".$date." / now: ".$noww."<br>";

    if($date < $noww){$expired = 'expired';}
  ?>
  <tr class="<?php echo $expired; ?>">


当使用PHP的内置日期功能时,您需要确保您使用的是。否则,
strotime()
将返回
false
DateTime()
将引发异常

要使用非标准的日期时间格式,您可以使用它来解析日期时间字符串并返回一个对象,您可以使用它来与其他
datetime
对象进行比较

// Date separated by dots
$date01 = \DateTime::createFromFormat('Y.m.d', '2017.04.18');

// Date with no separator
$date02 = \DateTime::createFromFormat('Ymd', '20170418');

// Get Unix timestamp
$timestamp = $date01->getTimestamp();

// Get MySQL format (ISO-8601)
$mysqlDate = $date02->format('Y-m-d');
因此,对于您的问题,您将执行以下操作:

$expires = \DateTime::createFromFormat('m/y', $card_history->expire);
$today   = new \DateTime();

if($expires < $today){$expired = 'expired';}
$expires=\DateTime::createFromFormat('m/y',$card\u history->expire);
$today=new\DateTime();
如果($expires<$today){$expired='expired';}
另请参见:


当我们不知道变量的内容时,我们无法复制;所以用户
var\u导出
。还显示实际正在运行的代码;这有语法错误。您不确定哪些变量?代码段中唯一没有设置的变量是$card_history->expire,我提到的是mm/yy检查这个