Jump to content

Total time users spent on page?


Monkuar

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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  :shrug:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

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.