Jump to content

[SOLVED] Time until next


DeanWhitehouse

Recommended Posts

How can i find out the time until the next event, example

 

The last event was 29 - 01 - 2009 (stored as a mysql timestamp)

 

and the next event is that timestamp plus a certain amount of days, i can get the date of the next event but what i want to get is the amount of time before the next date in days, then using that work out if it is in minutes hours etc.

 

So if the next event was in four hours it would say four hours not 0 days, but if the event was tomorrow it would say one day, etc.

 

This is my code so far (relevant part)

$next_maturation = date("d - m - Y",strtotime($account['maturation']." days +".$user_account['last_maturation']));//Next maturation date
echo $next_maturation - date("d - m- Y",strtotime("- ".$account['maturation']));//not working :s says - 4

Link to comment
Share on other sites

Ok, works with strtotime (it seems)

 

I tidied it up making it easier to read

function FormatTimeDiff($t1,$t2 = null,$format = 'yfwdhms')
{
$t2 = $t2 === null ? time() : $t2;
$s = abs($t2 - $t1);
$sign = $t2 > $t1 ? 1 : -1;
$out = array();
$left = $s;
$format = array_unique(str_split(preg_replace('`[^yfwdhms]`', '', strtolower($format))));
$format_count = count($format);
$a = array('y'=>31556926, 'f'=>2629744, 'w'=>604800, 'd'=>86400, 'h'=>3600, 'm'=>60, 's'=>1);
$i = 0;
foreach($a as $k=>$v)
{
	if(in_array($k, $format))
	{
		++$i;
		if($i != $format_count)
		{
			$out[$k] = $sign * (int)($left / $v);
			$left = $left % $v;
		}
		else
		{
			$out[$k] = $sign * ($left / $v);
		}
	}
	else
	{
		$out[$k] = 0;
	}
}
return $out;
} 

Link to comment
Share on other sites

Hmm when i do

print_r( FormatTimeDiff(strtotime($user_account['last_maturation']),strtotime($next_maturation)));

 

It says "

 Array ( [y] => 0 [f] => 0 [w] => 4 [d] => 2 [h] => 0 [m] => 0 [s] => 0 )

"

 

and doesn't seem to be counting minutes hours or seconds, or did i misunderstand the functions purpose?

Link to comment
Share on other sites

Hmm i am worried if this function works.

 

I am trying this

 

$time_dif = FormatTimeDiff(strtotime($next_maturation),strtotime(date("Y-m-d H:i:s")));

print_r($time_dif);

 

And it shows

Array ( [y] => 0 [f] => 0 [w] => 0 [d] => 0 [h] => -5 [m] => -58 [s] => -20 ) 

 

Yet the next maturation should of been 3 minutes ago, and therefore updated.

 

Let me explain in brief what i want to do.

 

I need to add interest at every maturation, for example if the maturation is one day add it every day, now as i don't know how i would use a cron job i am putting it in the page code.

 

I am checking to see if the next maturation is equal to or less than today and if so then update the db.

 

This is the relevant code

 

<?php
if(isset($_GET['account']))
{
$id = mysql_real_escape_string(((int) $_GET['account']));
$sql = "SELECT interest,name,maturation FROM nbanking_types WHERE id = '".$id."'"; 
$sql = mysql_query($sql);
if(mysql_num_rows($sql) == 0)
{
	echo "Account type Not Found";
}
else
{
	$account = mysql_fetch_assoc($sql);

	$sql = "SELECT last_maturation,balance FROM user_nbanking WHERE user_id = '".mysql_real_escape_string($_SESSION['user_id'])."' AND package_id = '".$id."'";
	$sql = mysql_query($sql);
	if(mysql_num_rows($sql) == 0)
	{
		mysql_query("INSERT INTO user_nbanking (user_id,package_id) VALUES ('".mysql_real_escape_string($_SESSION['user_id'])."','".$id."')");
		$sql = mysql_query($sql);
	}

	$user_account = mysql_fetch_assoc($sql);

	$next_maturation = date("Y-m-d H:i:s",strtotime($user_account['last_maturation']." + ".$account['maturation']." days"));//Next maturation date

	if($next_maturation <= date("Y-m-d"))
	{
		mysql_query("UPDATE user_nbanking SET balance = '".($user_account['balance'] + ($user_account['balance'] / 100 * $account['interest']))."', last_maturation = NOW() WHERE WHERE user_id = '".mysql_real_escape_string($_SESSION['user_id'])."' AND package_id = '".$id."'") or die(mysql_error());
		$sql = mysql_query($sql);
		$user_account = mysql_fetch_assoc($sql);
		$next_maturation = date("Y-m-d H:i:s",strtotime($user_account['last_maturation']." + ".$account['maturation']." days"));//Next maturation date
	}

	$time_dif = FormatTimeDiff(strtotime($next_maturation),strtotime(date("Y-m-d H:i:s")));
	print_r($time_dif);
	$new_time = "";

	if($time_dif['y'] < 0)
		$new_time .= " ".$time_dif['y'].($time_dif['y'] < 1 ? " years" : " year");
	if($time_dif['f'] < 0)
		$new_time .= " ".$time_dif['f'].($time_dif['f'] < 1 ? " months" : " month");
	if($time_dif['w'] < 0)
		$new_time .= " ".$time_dif['w'].($time_dif['w'] < 1 ? " weeks" : " week");
	if($time_dif['f'] < 0)
		$new_time .= " ".$time_dif['f'].($time_dif['f'] < 1 ? " months" : " month");
	if($time_dif['d'] < 0)
		$new_time .= " ".$time_dif['d'].($time_dif['d'] < 1 ? " days" : " day");
	if($time_dif['h'] < 0)
		$new_time .= " ".$time_dif['h'].($time_dif['h'] < 1 ? " hours" : " hour");
	if($time_dif['m'] < 0)
		$new_time .= " ".$time_dif['m'].($time_dif['m'] < 1 ? " minutes" : " minute");
	if($time_dif['s'] < 0)
		$new_time .= " ".$time_dif['s'].($time_dif['s'] < 1 ? " seconds" : " second");	

	$new_time = str_replace("-","",$new_time);	

For argument and testing sake i put the maturation period at one day and set my last maturation yesterday at 18:25 uk time, it is now 18:32 uk time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.