我有一个日期进入用户指定的循环。日期将始终来自格式化为“Y-m-d”字符串的数据库。我知道我可以直接比较字符串,只要它们是那种格式,但是,我也尝试使用strtotime转换日期来比较它们没有运气。我正在尝试确定用户在付款到期前有多少薪水
这就是我所拥有的
$due_date = '2016-12-13';
//count paychecks set to zero and added to by loop
$paychecks = 0;
//users next paycheck ('Y-m-d' ALWAYS)
$next_payday = $user['next_payday']; //equal to '2016-12-02'
//how often they get paid (int)
$frequency = 14;
while(strtotime($next_payday) <= strtotime($due_date)){
//next_payday equals 1480654800 when coming into the loop
//due_date equals 1481605200 when coming into the loop
//add 14 days to the date
$next_payday = date('Y-m-d', strtotime("+" .$frequency." days"));;
//add to paychecks
$paychecks++;
}
问题是循环永远不会停止。它一直在前进和前进。
感谢任何人都能给我的帮助。
答案 0 :(得分:1)
啊,一定要使用strtotime获取整数(表示epoch以来的秒数)进行比较,然后将天数乘以一天中的秒数(86400):
$due_date = strtotime('2016-12-25');
//count paychecks set to zero and added to by loop
$paychecks = 0;
//users next paycheck (unixtime for comparison)
$next_payday = strtotime($user['next_payday']);
//how often they get paid (int)
$frequency = 14;
while($next_payday <= $due_date){
//add 14 days to the date
$next_payday += ($frequency * 86400);
//add to paychecks
$paychecks++;
}