FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Date/Time Entries (PHP & MySQL)





Horizon
Well I'm having a bit of trouble again, and this time it doesn't include detailed info on MyBB xD

Within a database, I've got a row that has time in it. Time that derived from:

$time = date("gis");

The time in the Database doesn't change. What I need is for a way to detect if the CURRENT time exceeds the time in the database by a specific number. I just can't find a way using if statements.. Unless I'm just brain-dead atm or something.

Just remember, the format would be:

g - Hours (1-12)
i - Minutes (1-66)
s - Seconds (0-66)

But that's automatic with date("gis");.

And just as an example:

Database holds the time: 22000 (or 2:20:00 - 2 hours, 20 minutes, 0 seconds)
Current time is: 22700 (or 2:27:02 - 2 hours, 27 minutes, 0 seconds)
TIME THAT MUST ELAPSE: 10 minutes

This example will 'fail' because it has not been 10 minutes, it's only been 7 minutes and will not run a script because it hasn't been 10 minutes yet.

Database holds the time: 22000 (or 2:20:00 - 2 hours, 20 minutes, 0 seconds)
Current time is: 24800 (or 2:27:02 - 2 hours, 48 minutes, 0 seconds)
TIME THAT MUST ELAPSE: 10 minutes

This example is successful because it's been 28 minutes, which is more then 10 minutes and will run a specified script because of it being more then 10 minutes.

So.. can anybody help? I don't need to know a script that is a result of the time exceeding, I just need to know a way of detecting if the time is exceeded.
guitarcrazy087
Ok, well, I'm a php noobie but I'll try to give you some ideas.

When you insert your time into your database make sure you insert it like this:

date("g:i:s");

Then to compare the present time and the old time-

Retrieve the date from the database and explode it like this:

$date = explode(":", your_date);

So now you have an array where $date['0'] is the hour, $date['1'] is the minute and $date['2'] is the second.

Then your going to have to do the same thing with the present time:

$presdate = date("g:i:s");
$presdate = explode(":", $presdate);

Then I would start by comparing the hours:

if ($presdate['0'] > $date['0']) {
echo "you pass";
}
else if ($presdate['1'] - 10 > $date['1'] ) {
echo "you pass";
}
else {
echo "you do not pass";
}

That's the basics I believe. Of course if you really want to check this properly your going to have to check the date too, not just the time.

Hope this help!

p.s. There may be some glitches here so anyone more knowledgeable please give their input!
rvec
do you know mysql has date, datetime and time data types?
If you save the time in the time data type you could save it in the format: date('H:i:s') (not g, that's without the leading zero and may cause problems).

Then you can compare the time in mysql.
For your example
$sql = 'SELECT * FROM table WHERE time<'.date('H:i:s', time()-(10*60));
that would select the rows where the time is more than 10 min ago.

You could also save the time in the int data type with the unix timestamp (time()), that wouldn't be much different from the above.

$sql = 'SELECT * FROM table WHERE time<'.time()-(10*60);

but in both cases you'd get different return values than in your current script, so I don't know if it'd be worth rewriting it.

The script by guitarcrazy087 wouldn't always work. If the last run was on 23:59 it would run again on 00:00 (next day) and in all other cases it would run after 11 min instead of 10 (> should be >=).

I think the best and easiest way to make it work with your current format would be to convert it to the unix timestamp format.
Code:
$date=date("gis"); //here put in your date from the database
if (strlen($date)==5 )
{
 $hour = $date[0];
 $min = $date[1].$date[2];
 $sec = $date[3].$date[4];
}
elseif (strlen($date)==6 )
{
 $hour = $date[0].$date[1];
 $min = $date[2].$date[3];
 $sec = $date[4].$date[5];
}
else
{
 //this should never happen as the date is always 5 or 6 characters long but just in case
 die('strange error happening, please try again');
}
$date = getdate();
$time = mktime($hour, $min, $sec, $date['mday'], $date['mon'], $date['year']);

if (($time+10)<=time())
{
 //here the time is 10 min or more ago
}
else
{
 //here the time is less than 10 min ago
}
guitarcrazy087
Yes, I know my script wouldn't work all the time as you pointed out. That's why I suggested incorporating the actual date, not just the time, like you just showed...
rvec
hmm I forgot something in my script, this way it also won't work.

before
Code:
if (($time+10)<=time())

There should be this part:
Code:
if ($time>time())
{
$time = mktime($hour, $min, $sec, $date['mday']-1, $date['mon'], $date['year']);
}

to make sure the time is in the past and not in the future. The only downside here is if you try to update 24 hours and 1-9 min after the last update it will give the wrong result.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.