Log in

View Full Version : Resolved time converter



auriaks
11-10-2009, 11:28 PM
Hi, maybe someone can help me with this...
I have
include('db_conn.php');
$queryget = mysql_query("SELECT * FROM reg_users ORDER BY `time` LIMIT 10") or die("Error with query");
while ($row = mysql_fetch_array($queryget))
{
$date = $row['date'];
$time = $row['time'];
$datet = date("Y-m-d");
$timet = date("H:i:s");

time's are saved as you can see: H:i:s (23:10:55)
so, i need to convert $time = $row['time']; and $timet = date("H:i:s"); to seconds... is there an option? THANKS

Nile
11-11-2009, 12:11 AM
You can do this two ways (I suggest the second)

#1
(To get seconds:)


$seconds = explode(':', $timet)[2];

#2 (highly suggested)
Instead of saving times as H:i:s, I would save times($row['time']) using the time() (http://us.php.net/manual/en/function.time.php) function. Then when getting the time as H:i:s you would do:


$time = date("H:i:s", $row['time']);
echo $time; // 23:10:55
$seconds = date("s", $row['time']);
echo $seconds; //55


Feel free to ask questions.

auriaks
11-11-2009, 12:19 AM
what does it means [2] ?? because of that i got an error and i cant open my page

Nile
11-11-2009, 12:59 AM
Sorry. :/


$seconds = explode(':', $timet);
$seconds = $seconds[2];

auriaks
11-11-2009, 02:57 PM
it works, but takes only seconds. I need all time convert to seconds like 2H 5m 32s = 7532 seconds :)

auriaks
11-11-2009, 09:25 PM
what i have to write?

Nile
11-11-2009, 11:47 PM
Haha, sorry:


$total_seconds = 0;
$seconds = explode(':', $timet);
$total_seconds += $seconds[0] * (60 * 60);
$total_seconds += $seconds[1] * 60;
$total_seconds += $seconds[2];
echo $total_seconds;


It is not tested!

auriaks
11-26-2009, 07:44 PM
Yea, it's working :)

auriaks
01-03-2010, 10:41 PM
Hi,
I faced problem of this function, but don't know where :)
I use this: [also I update their mysql time in every move]


...

function timeDiff($firstTime,$lastTime) {
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
$timeDiff=$lastTime-$firstTime;
return $timeDiff;
echo "$timeDiff";
}

while ($row = mysql_fetch_array($queryget))
{
$nick = $row['nick'];
$time = $row['time'];
$timet = date("H:i:s");
$id = $row['id'];

if (timeDiff($time,$timet) <= 300) {
if (timeDiff($time,$timet) >= -1) {
$var = '1';
echo "
$nick user is online
";
}}}
if ($var < 1) {echo "noone is online";}


and no users are online ALWAYS.
Thanks :)

Nile
01-03-2010, 11:14 PM
First of all


return $timeDiff;
echo "$timeDiff";

Onc you return something, the whole function is stopped.

Second of all

if (timeDiff($time,$timet) <= 300) {
if (timeDiff($time,$timet) >= -1) {

Whats the point of that, just do:


if ((timeDiff($time,$timet) <= 300) AND timeDiff($time,$timet) >= -1) {


Next,


$var = '1';

Even though PHP works with strings as numbers, you may want to do:


$var = 1;

Instead.

And finally - what problem are you facing?

auriaks
01-04-2010, 03:55 PM
I don't know it just worked, but now something is wrong. What I was trying to create was online system. I have two times and two dates: $time & $date - current time and date; $timet & $datet - time and date when user made move in past or maybe now.

I need to count seconds of $time, $date and $timet, $dated to see which is higher.
if xtimetdated - xtimedate >= -300 it will show online.

time formats:

$datet = date("Y-m-d");
$timet = date("H:i:s");

same as $date and $time

Nile
01-04-2010, 06:09 PM
I can't help until I know what is wrong. Can I see an example to your page, also can you please tell me whats supposed to happen from a users point of view.

Thanks! ~Nile

auriaks
01-04-2010, 06:18 PM
Here (http://www.laisvasprotas.xz.lt) in the end of page... Every USER will see online nicks of others in green color. When theres no user online it shows black message

Nile
01-04-2010, 06:20 PM
So - whats the problem? You don't want the black message?

auriaks
01-04-2010, 06:34 PM
The problem is in all system... I give a good time everytime, but somehow its not working. My problem is, that when i log into it shows that I am online and after 6 minutes its not. Somehow, time is counting in bad way...

Nile
01-04-2010, 06:38 PM
Sorry - I didn't completely understand. 6 minutes after you log in - it shows whos online?

auriaks
01-04-2010, 06:48 PM
yes, 6 minutes it shows that im online. also I can see other online users.

can you understand my script which i posted? what it does...

Nile
01-04-2010, 06:54 PM
Well, I can't help you, I'm very sorry. Try making a new thread - because this thread is old, and users don't want to post in it [also, the question your asking doesn't have anything to do with the original thread]

Sorry and good luck!

auriaks
01-04-2010, 07:00 PM
Ok, let's go back few steps... :)

I have
$date = date("Y-m-d");
$time = date("H:i:s");

How I can count(convert) date and time into seconds?

auriaks
01-04-2010, 07:02 PM
Ok, let's go back few steps... :)

I have
$date = date("Y-m-d");
$time = date("H:i:s");

How I can count(convert) date and time into seconds?

Nile
01-04-2010, 07:06 PM
<?php
function secondsConvert($time){
$seconds = explode(':', $time);
$total_seconds = 0;
$total_seconds += $seconds[0] * (60 * 60);
$total_seconds += $seconds[1] * 60;
$total_seconds += $seconds[2];
return $total_seconds;
}
$time = date("H:i:s");
echo secondsConvert($time)." seconds";
?>

secondsConvert will return (hours*60*60)+(minutes*60)+seconds.

auriaks
01-04-2010, 07:27 PM
how to do with date?

Nile
01-04-2010, 07:38 PM
Well - for days, that would just be day * 24 * 60 * 60, but for months and years, it'd be hard because there aren't always four weeks in a month...

auriaks
01-05-2010, 02:18 AM
how to create difference then?

can I use $time - $timet = 300seconds ?

OR

if (($time - $timet) >= '00:05:00') then {echo "he is online";}

auriaks
01-05-2010, 11:04 PM
How to do something like that?

Nile
01-05-2010, 11:08 PM
What do you mean? Can't you just insert names of the people online, and when the user logs out, there not online?

auriaks
01-07-2010, 03:35 PM
But sometimes people dont push log out and name would be still there

Nile
01-08-2010, 02:20 AM
Hmm.. So every time the user goes somewhere, have a field for each user called "Minute" and insert the current minute. Then, when every user refreshes the page, it checks each users minutes, and if its more then 5 minutes idle, delete them from the list. (also, make sure 61 = 0, 62 = 1...)

auriaks
01-08-2010, 08:15 AM
Will this work even then when they close their browsers??

Nile
01-08-2010, 01:01 PM
Yes - 5 minutes after they close the browser, they shold be off the list.

auriaks
01-08-2010, 03:19 PM
One more thing, if noone refreshes that page until tomorrow same time? page will not delete it...

auriaks
01-09-2010, 11:25 PM
Can someone tell me how the online system works on other sites??

Nile
01-10-2010, 01:50 AM
Well. :rolleyes: If no one is using/on the site - no one will be looking at the whos online page, so there's not even a point of removing the user when no ones online.

auriaks
01-10-2010, 03:39 AM
eg: two(one, three) users closes their IE windows likely at the same time 10:30.
Then noone goes online until next day same time...
If i will go online, I will see them online... (but they are logged off)
5 minutes doesnt know what day is today... and it will be shown everyday same time. Offcourse, its quite impossible that noone will come online in one day, but still - bug :D ;D

Nile
01-10-2010, 03:53 AM
Ok, so you would insert:
14 - 9:50

In the database, 14 being the date (thurs, jan. 14th), and 9:50 being the time. If the current date doesnt equal 14, there not online.