DeanWhitehouse Posted January 29, 2009 Share Posted January 29, 2009 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 https://forums.phpfreaks.com/topic/142882-solved-time-until-next/ Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 a google search found this: http://us.php.net/manual/en/function.time.php#83560 Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749102 Share on other sites More sharing options...
corbin Posted January 29, 2009 Share Posted January 29, 2009 If it's in a MySQL table, why not just do the calculation with MySQL? Or, you could pull it as a unix timestamp to be able to work with it more easily (UNIX_TIMESTAMP()). Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749106 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 How would i do it with mysql ? Or with that function you found rhodesa it doesn't say how the time should be formatted, just strtotime? Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749111 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 with that function you found rhodesa it doesn't say how the time should be formatted, just strtotime? i would assume timestamps Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749112 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 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 https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749113 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 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 https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749119 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 what is the output of: <?php print $user_account['last_maturation'] . '<br>'; print $next_maturation . '<br>'; print_r(FormatTimeDiff(strtotime($user_account['last_maturation']),strtotime($next_maturation))); ?> Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749165 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 Prints 2009-01-28 18:07:54 2009-01-29 18:07:54 Array ( [y] => 0 [f] => 0 [w] => 0 [d] => 1 [h] => 0 [m] => 0 => 0 ) Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749875 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 right...so it's 1 day exactly between the two...what would you expect? Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749878 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 omg, sorry , i was being blond in my logic i was trying to get the amount of time until the next date, but was instead coding it to get the difference between the wrong dates Thanks for the help Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749883 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 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 https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749899 Share on other sites More sharing options...
redarrow Posted January 29, 2009 Share Posted January 29, 2009 Dosent the date format, have to be the same in your if statements. Y == year -m ==month -d == day H:== hour i:== min s == seconds for the if statement to work Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749905 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 Hmm that is true, i changed it but still no luck Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749908 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Author Share Posted January 29, 2009 Seems to be working now, after a bit of changing around Link to comment https://forums.phpfreaks.com/topic/142882-solved-time-until-next/#findComment-749918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.