how many days left

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

how many days left

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

recent versions of mysql support the function date_diff, see http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Thanks for such fast answer :D

I love this forum :D
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Also, this function is supported by MySQL 4.1 versions :)
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post 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);
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post 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);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
}
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

thanks :D It's a lot better now :)
Post Reply