2013-05-22 120 views
0

我有2个变量被设置是这样的:Symfony的日期变量if语句

 <?php $current = strftime('%m/%d/%g %H:%M:%S')?> 
     <?php $target = strftime('%m/%d/%g %H:%M:%S', strtotime($item->getendtime()))?> 

然后,我有一个if语句是这样的:

 <?php if ($current < $target): ?> 
     Do this 
     <?php else: ?> 
     Do that 
     <?php endif; ?> 

,直到我们在新的一年,它工作正常。例如,我有几件物品在2012年有一个“endtime”。如果if语句按照它应该的那样工作,它应该“做那件事”,而不是它“做这个”

+0

如果您在使用本主义与PHP> = 5.3,你可以比较日期是这样的:'新的DateTime()< $item-> getDateTimeObject( 'END_TIME')' – 1ed

回答

2

使用下面的替代与UNIX时间戳):

$current = time(); 
$target = strtotime($item->getendtime()); 
+0

如果我尝试不使用“的strftime “那么$ current字符串会返回一个大整数。 $ item-> getendtime()作为日期存储在数据库中,而不是整数。 – djcloud23

+0

这就是为什么您使用'strtotime'函数将数据库中的日期更改为这个大整数(UNIX时间戳)的原因。 –

+0

工作很好。这次真是万分感谢。 – djcloud23