Ricardo
Ricardo

Reputation: 1672

Printing how much days left to a subscription?

I have the next question: I have a table with a 'datetime' field in it called "last_payment", the thing i want to do is to show the users how much days left to their subscription (its a monthly subscription) so basically if the value in last_payment is "2011-06-10 12:28:11" and today its 15/06/2011 i want it to show that they have 25 days left.

I have the current code and it doesn't matter how much time i played with it it doesn't work when im trying to pull the data from the MySQL but it works when i do a "mktime()" function..:

$today = time();
$row_ct = mysql_fetch_array($result_ct);

//Subscription Counting Funcrion for Clients
$cdate_c = mktime(0, 0, 0, 12, 31, 2009, 0);
$difference_c = $today - $cdate_c;
if ($difference_c < 0) { $difference_c = 0; }

The following aren't working:

$cdate_c = mktime($row_ct["last_payment"]);
$cdate_c = mktime(strtotime($row_ct["last_payment"]));
$cdate_c = date(strtotime($row_ct["last_payment"]));

Some help?

Upvotes: 1

Views: 457

Answers (2)

Josh
Josh

Reputation: 8191

mktime accepts 7 parameters, and returns false on invalid parameters. You only give it 1, so that is why these don't work:

$cdate_c = mktime($row_ct["last_payment"]);
$cdate_c = mktime(strtotime($row_ct["last_payment"]));

date accepts a format (how you want your time to look), and an optional timestamp. That is why you don't get anything for this:

$cdate_c = date(strtotime($row_ct["last_payment"]));

Try this related article: How to calculate the difference between two dates using PHP?

Convert your dates to timestamps, calculate the difference in seconds, then calculate the number of seconds per day to determine the number of days for the difference.

Upvotes: 1

Stuck
Stuck

Reputation: 12292

Try

$cdate_c = strtotime($row_ct["last_payment"]);

Upvotes: 1

Related Questions