| ||||||
|
How to calculate how much time a subscription has left in PHP
To calculate the remainder of a subscription time in PHP we need three items: the end date from the future in timestamp format, the current date in timestamp format, and a function to convert the difference between the two into something that is actually understandable to a human who's brain can not figure out how many days are within a given thousand seconds. First then we will need to find the expiry date in Unix time. Here is an example of how to output a timestamp that is 10 months in the future, at midnight. In reality you would want to read this code as a fixed variable from a MySQL database for each individual user: $futureTime = mktime(0, 0, 0, date("m") + 10, date("d"), date("Y")); Next up we need the current date, and to calculate the difference between the future and now: //Get current Unix time stamp $currentTime = time(); //Calculate the difference in seconds $difference = $futureTime - $currentTime; After that we will call upon a time conversion function, to change the seconds into days, hours, minutes, and seconds. Strftime() will not work in this instance, as it is based around the counting seconds of the unix era. If you use it as a function to convert the difference therefore, you will end up with a date in the mid 70s, instead of the time that you were after! function convertTime($difference) { //Calculate how many days are within $difference $days = intval($difference / 86400); //Keep the remainder $difference = $difference % 86400; //Calculate how many hours are within $difference $hours = intval($difference / 3600); //Keep the remainder $difference = $difference % 3600; //Calculate how many minutes are within $difference $minutes = intval($difference / 60); //Keep the remainder $difference = $difference % 60; //Calculate how many seconds are within $difference $seconds = intval($difference); //Output: return "Days: ".$days." Hours: ".$hours." Minutes: ".$minutes." Seconds: ".$seconds; } Here then is what you get when you put the whole code together: <?php //For our example the future time will be 10 months away. //In the real world this needs to be a fix point in time. $futureTime = mktime(0, 0, 0, date("m") + 10, date("d"), date("Y")); //Get current Unix time stamp $currentTime = time(); //Calculate the difference in seconds $difference = $futureTime - $currentTime; $remainder = convertTime($difference); echo "Time left until your subscription runs out:<br><br>".$remainder; function convertTime($difference) { //Calculate how many days are within $difference $days = intval($difference / 86400); //Keep the remainder $difference = $difference % 86400; //Calculate how many hours are within $difference $hours = intval($difference / 3600); //Keep the remainder $difference = $difference % 3600; //Calculate how many minutes are within $difference $minutes = intval($difference / 60); //Keep the remainder $difference = $difference % 60; //Calculate how many seconds are within $difference $seconds = intval($difference); //Output: return "Days: ".$days." Hours: ".$hours." Minutes: ".$minutes." Seconds: ".$seconds; } ?>
|