Page 1 of 1

how many days left

Posted: Fri Dec 22, 2006 6:16 am
by arukomp
Hello,

I've done all functions in my program, except for one:

I want my members to see how many days they have left to use my program features.

I think i need to set upgrade finish time and insert into MySQL db. Then every time user logs in, time between now and upgrade end is calculated and displayed to the member.

Now what I want to know is how to set destination time and how to calculate how much time has been left till end. I want to display only days and set time only with year, month and day. So hours, seconds and minutes won't be needed.

Thanks for any suggestions.

Posted: Fri Dec 22, 2006 6:20 am
by volka
recent versions of mysql support the function date_diff, see http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html

Posted: Fri Dec 22, 2006 6:28 am
by arukomp
Thanks for such fast answer :D

I love this forum :D

Posted: Fri Dec 22, 2006 6:32 am
by arukomp
Also, this function is supported by MySQL 4.1 versions :)

Posted: Fri Dec 22, 2006 8:09 am
by arukomp
Problems comming :(

when calculating the time between two dates and echo'ing it, only "recource id #7" comes out and no result of calculation. Here's the code I wrote:

Code: Select all

function daysleft($userid){
	$sql = "SELECT * FROM users WHERE id='$userid'";
	$res = mysql_fetch_array(mysql_query($sql));
	$date = $res['upgrade'];
	$sql = "SELECT DATEDIFF('$date', NOW())";
	$result = mysql_query($sql);
	return $result;
}

echo daysleft(1);

Posted: Fri Dec 22, 2006 8:26 am
by arukomp
Solved. Man, i have to ask less and search more :)

To those who is interested in answer:

Code: Select all

function daysleft($userid){
	$sql = "SELECT * FROM users WHERE id='$userid'";
	$res = mysql_fetch_array(mysql_query($sql));
	$date = $res['upgrade'];
	$sql = "SELECT DATEDIFF('$date', NOW())";
	$result = mysql_query($sql);
	$res = mysql_result($result, 0);  // use this to fetch first row(0) of a result($result)
	return $res;
}

echo daysleft(1);

Posted: Fri Dec 22, 2006 8:38 am
by volka
Why do you fetch the stored date only to use it in another query?

Code: Select all

function daysleft($userid){
  $sql = "SELECT DATEDIFF(upgrade, NOW()) FROM users WHERE id='$userid'";
  $result = mysql_query($sql) or die(mysql_error();
  return mysql_result($result, 0);
}

Posted: Fri Dec 22, 2006 9:01 am
by arukomp
thanks :D It's a lot better now :)