Monkuar Posted February 12, 2012 Share Posted February 12, 2012 I have a Yahtzee system session_start(); $_SESSION['Yahtzee']['totaltime'] =time(); echo $_SESSION['Yahtzee']['totaltime']; Now, Long STORY Short when somone finishes playing the Yahtzee, I update there username with the score they had, and I want to update how long they have been playing, and it will be for "Total Time Playing globally" no matter how many games. If I do this session and echo it out, it echo's out the time, but I need it to echo out seconds instead so I can just add that to my totaltime field in my database each time they finished a game. Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/ Share on other sites More sharing options...
requinix Posted February 12, 2012 Share Posted February 12, 2012 Assuming that totaltime was previously set, just subtract that value from time(). time() - $_SESSION['Yahtzee']['totaltime'] Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317133 Share on other sites More sharing options...
Monkuar Posted February 12, 2012 Author Share Posted February 12, 2012 $_SESSION['Yahtzee']['totaltime'] = time(); $starttime = $_SESSION['Yahtzee']['totaltime']; $totaltime = time() - $starttime; echo $totaltime; still showing 0 hmmmmmmmm Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317134 Share on other sites More sharing options...
requinix Posted February 12, 2012 Share Posted February 12, 2012 Yeah... Because you're overwriting the previous value. Right there on the first line. Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317136 Share on other sites More sharing options...
Monkuar Posted February 12, 2012 Author Share Posted February 12, 2012 Yeah... Because you're overwriting the previous value. Right there on the first line. session_start(); $_SESSION['Yahtzee']['time'] = time(); if (isset($_GET['ez'])){ echo "hey"; $time2 = $_SESSION['Yahtzee']['time2']=time(); $totalTime = $_SESSION['Yahtzee']['time'] - $time2; echo $totalTime; exit; } This doesn't work? Any idea Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317137 Share on other sites More sharing options...
Psycho Posted February 12, 2012 Share Posted February 12, 2012 When a user first starts the game, set the start time like so $_SESSION['Yahtzee']['start_time'] = time(); Then when the user finishes the game, calculate the total time like so $total_time = time() - $_SESSION['Yahtzee']['start_time'] = time(); Then do whatever you want with that value Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317139 Share on other sites More sharing options...
Monkuar Posted February 12, 2012 Author Share Posted February 12, 2012 Session star is my code where that users starts the game. session_start(); $_SESSION['Yahtzee']['start_time'] = time(); if (isset($_GET['ez'])){ $total_time = time() - $_SESSION['Yahtzee']['start_time'] = time(); echo $total_time; exit; } So he starts the game, then I will be using $_GET or $_POST to update there value, I wait 5seconds on main page, go to ?ez=1 on URL to activate my $_GET['ez'] to show the time spent and it still shows 0? I am obviously doing something wrong because your code usually never fails Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317140 Share on other sites More sharing options...
jason310771 Posted February 12, 2012 Share Posted February 12, 2012 session_start(); if (!isset($_SESSION['Yahtzee']['start_time'])) { $_SESSION['Yahtzee']['start_time'] = time(); } if (isset($_GET['ez'])){ $total_time = time() - $_SESSION['Yahtzee']['start_time']; // = time(); echo $total_time; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/256920-total-time-users-spent-on-page/#findComment-1317184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.