Jump to content

[SOLVED] time()


supanoob

Recommended Posts

Just save the seconds returned by time() when the action happens, and then do a subtraction to see how long ago it happened:

 

<?php
$saved_time = 1217191386; //e.g.
$diff = time() - $saved_time;
echo "Action happened $diff seconds ago.";
?>

Link to comment
Share on other sites

Well, you'll need to do a comparison then won't you? You'll need both times in the form of integers. So you'll have to use strtotime()/mktime() to convert your dates to unix timestamps.

 

If you're trying to do it in a mysql query, use the mysql UNIX_TIMESTAMP() function.

 

Perhaps if you explain your situation more fully, we'll be able to give you more help.

Link to comment
Share on other sites

So an action never happened the previous day? If no, you can convert the HH:MM:SS format to a timestamp with strtotime(). The function assumes the provided time is today, so we won't need to supply todays date:

 

<?php
$saved_time = '12:00:00';
$time = strtotime($saved_time);
$diff = time() - $time;
echo "Action happened $diff seconds ago.";
?>

Link to comment
Share on other sites

Well basically i have a forum, i want to make it so each person can post once every 30 seconds. I have the persons last post time saved into the db, i then want to compare both the current time and last post time to see if 30 seconds has passed yet. if not i want to say so and stop the post being made.

Link to comment
Share on other sites

So an action never happened the previous day? If no, you can convert the HH:MM:SS format to a timestamp with strtotime(). The function assumes the provided time is today, so we won't need to supply todays date:

 

<?php
$saved_time = '12:00:00';
$time = strtotime($saved_time);
$diff = time() - $time;
echo "Action happened $diff seconds ago.";
?>

 

That has sorted it, thanks alot.

 

Topic solved :D

Link to comment
Share on other sites

But if a user's latest post was made some day in the past (will happen all the time, obviously), you'll need to save the date also. Or simply the timestamp.

 

yeah i do, when a post is made the time is saved. I think i see what you mean.

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.