CancelImage Upload

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;

}

?>


Need help with some code? Post a help thread as guest:
Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder
Posted by Garren Harland on August 17, 2009, 5:08 pm.
Although regular expressions always look impressive, and are a good way of confusing non-programmers (and some programmers) while coming across as a rocket scientists, there are some instances when regular expressions are indeed not required. And validating a date is one of them.
Click here to read on.
Posted by Garren Harland on August 17, 2009, 4:17 pm.
Let's say you want to plan your Easter weekend for 2016. Naturally enough to do this you will require the exact date that Easter falls on. You can now do two things. Go look up the date in a calendar, or simply make use of a predefined PHP function capable of returning every Easter that has or will occur during the Unix era.
Click here to read on.
Posted by Garren Harland on August 3, 2009, 6:23 pm.
This page discuses how you can trick the PHP function mktime() into returning the Unix timestamp of a date within a specified distance in the future or from the past. To first illustrate this we will use the following example.
Click here to read on.
Posted by Garren Harland on August 3, 2009, 10:15 am.
Most programmers go about this task the wrong way. Or rather they only complete half the job, and then wonder why they are not getting the intended results. Usually they do something along the lines of subtracting an old timestamp from the current one, which is actually a step in the right direction, but then they get frustrated when strftime() returns them some date from the 70s.
Click here to read on.
Posted by Garren Harland on July 30, 2009, 5:57 pm.
Since the 1st of January 1970 the Unix clock has been ticking, one second at a time. Thanks to this every programmer has one time format from which all the others can be derived. In PHP this timestamp (with the current date, time, etc) can be loaded via the function.
Click here to read on.
Posted by Garren Harland on July 30, 2009, 5:12 pm.
Timestamps are great for the following reason. They can be converted into any chosen format without having to do much pre-converting. This saves a lot of time compared to times and dates stored in human readable formats.
Click here to read on.
Posted by Garren Harland on July 30, 2009, 4:26 pm.
Using the PHP function date() the current date and time can displayed numerous ways. Here then is a reference guide to help demonstrate these numerous formats and their respective codes.
Click here to read on.
Posted by Garren Harland on July 6, 2009, 2:36 pm.
Here is how you find out if the current year is a leap year with PHP. The code date("L") will either return a 0 if it is not currently a leap year, or a 1 if it does happen to be one.
Click here to read on.
Everyday English GmbH • 8105 Regensdorf, Switzerland • Tel: +41 (0)44 840 03 68 • www.mydesigntool.cominfo@mydesigntool.com