Jump to content

Archived

This topic is now archived and is closed to further replies.

DaVuLf

Time

Recommended Posts

You know how forums use a variable for the posted time that is constant for whoever looks at it? (i mean, it is affected by time zones, but the basic time in GMT is there) Well, I was wondering how you get that number from somewhere.

I want to be able to get the time, and then check conditions against it. Is there any way to do this? What i'm intending is that I can run if statements off of the time.

[code]
if ($time > 6:00:00}{
     $night=true;
} else {
     $night=false;
}
[/code]

That type of thing. Or what about:

[code]
if(($time_posted - $time_now) < 00:00:60){
     echo "You haven't waited a minute before posting again.";
} else{
     post;
}
[/code]

Thanks for any help :).

Share this post


Link to post
Share on other sites
I believe it stores the posted time in SQL. You would need to send $time_posted to a database, then have it pull it out.

$time_now can obviously be done with date function

Share this post


Link to post
Share on other sites
[!--quoteo(post=375281:date=May 19 2006, 01:00 PM:name=Invincible)--][div class=\'quotetop\']QUOTE(Invincible @ May 19 2006, 01:00 PM) [snapback]375281[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I believe it stores the posted time in SQL. You would need to send $time_posted to a database, then have it pull it out.

$time_now can obviously be done with date function
[/quote]

Invincible, thanks for the reply. I'm aware that I need to have it stored in a database, but I'm not sure how to 'get' those numbers. How do you use the date function? Is there any documentation on it?

Thanks,
DaVuLf

Share this post


Link to post
Share on other sites
[!--quoteo(post=375297:date=May 19 2006, 02:17 PM:name=DaVuLf)--][div class=\'quotetop\']QUOTE(DaVuLf @ May 19 2006, 02:17 PM) [snapback]375297[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Invincible, thanks for the reply. I'm aware that I need to have it stored in a database, but I'm not sure how to 'get' those numbers. How do you use the date function? Is there any documentation on it?

Thanks,
DaVuLf
[/quote]

check out the manual for the most help on the date function. however, in and of itself, it won't be a whole lot of use to you for checking things. here are four great functions that used in conjunction will allow you to do just about anything with times:

[a href=\"http://www.php.net/time\" target=\"_blank\"]time()[/a]
[a href=\"http://www.php.net/mktime\" target=\"_blank\"]mktime()[/a]
[a href=\"http://www.php.net/strtotime\" target=\"_blank\"]strtotime()[/a]
[a href=\"http://www.php.net/date\" target=\"_blank\"]date()[/a]

time, mktime and strtotime return a UNIX timestamp that can then be used with date() to display however you like. if you want to read up on each of them, i've linked to the corresponding manual page for each above. once you've gotten a feel for how they work and you have a more specific question about how to do something, let me know, and i'm sure we can help you work through it.

dates and times can be some of the most tricky problems in working with PHP, but they can also be the most rewarding when you get a handle on it.

good luck!

Share this post


Link to post
Share on other sites
Obsidian, thanks for all that. I read through some of that, but I also found a tutorial that seems to help out. Now, I have got a question to do with this code:

[code]
function date_diff($str_start, $str_end)
{

$str_start = strtotime($str_start); // The start date becomes a timestamp
$str_end = strtotime($str_end); // The end date becomes a timestamp

$nseconds = $str_end - $str_start; // Number of seconds between the two dates
$ndays = round($nseconds / 86400); // One day has 86400 seconds
$nseconds = $nseconds % 86400; // The remainder from the operation
$nhours = round($nseconds / 3600); // One hour has 3600 seconds
$nseconds = $nseconds % 3600;
$nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
$nseconds = $nseconds % 60;

echo $ndays." days, ".$nhours." hours, ".$nminutes." minutes, ".$nseconds." seconds.";

}
[/code]

Why can't I access $nminutes or any of the other variables from outside of the function? How would I take those out of the function? I need to refer to these further down the file here:

[code]
$str_time= $nminutes; //.":". $nseconds;
echo $str_time;
$worth = $quantity*$value;
$query = "INSERT INTO $team VALUES ('','$team','$stock','$type','$str_time','$quantity','$value','$worth','$balance')";
mysql_query($query);
[/code]

Thanks for all the help so far. :)

Share this post


Link to post
Share on other sites
I figured it out.. In case anyone was wondering:

[code]
function date_diff($str_start, $str_end)
{
global $nminutes, $nseconds;
$str_start = strtotime($str_start); // The start date becomes a timestamp
$str_end = strtotime($str_end); // The end date becomes a timestamp

$nseconds = $str_end - $str_start; // Number of seconds between the two dates
$ndays = round($nseconds / 86400); // One day has 86400 seconds
$nseconds = $nseconds % 86400; // The remainder from the operation
$nhours = round($nseconds / 3600); // One hour has 3600 seconds
$nseconds = $nseconds % 3600;
$nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
$nseconds = $nseconds % 60;

echo $ndays." days, ".$nhours." hours, ".$nminutes." minutes, ".$nseconds." seconds.";

}
[/code]

Thanks :).

Share this post


Link to post
Share on other sites

×

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.