I am trying to report how many hours are remaining of a users subscription. The format of date/time which I am using is Y-m-d H:i:s
. Basically I log the time the user pays in the database, and assume the plan will expire 1 day from when the payment was made.
What is wrong with my logic here in the calculation of difference between the current time and the end date?
$start_time = strtotime($callback->_time_paid());
$end_time = 0;
$end_time = strtotime(date('Y-m-d H:i:s', strtotime($callback->_time_paid().(' +'.$days. 'day'))));
$static = ($end_time - $start_time);
$time = (strtotime(date('Y-m-d H:i:s')) - $static) - $static;
print_r($time);
$hours = floor($time / 3600);
$minutes = floor($time / 60);
Not sure why you are subtracting the difference in seconds twice, or the math on your conversions but I've not had enough coffee yet.
Here's what works for me - get your timestamp from DB, if it is in the future then do some math.
<?php
$end_time = strtotime(date('Y-m-d H:i:s', strtotime($callback->_time_paid().(' +'.$days. 'day'))));
if($end_time>(time())){
// how many seconds are we dealing with?
$seconds=$end_time-(time());
$hours=(int)($seconds/3600);
$min=(int)(($seconds%3600)/60);
$sec=(($seconds%3600)%60);
print($hours." ".$min." ".$sec);
}else{
print("Expired already");
}
?>