Jump to content

Time functions with mysql & php


gerkintrigg

Recommended Posts

hi!
I'm trying to work out how to format time using PHP and mysql.

I have a timestamp field in a database and I want to use PHP to display how many days are left before it needs to be removed. The limit is 30 days. So I'd like the php to display "X no of days remaining" where X no is the actual number.

I've tried using strtotime() and adding 30 days to the timestamp. I have NO IDEA how to start this.

Could anyone offer some suggestions please?
Link to comment
Share on other sites

Just use mysql:

[code]SELECT id, DATEDIFF(columnname, NOW()) AS time_in_db FROM tablename[/code]

Then loop through the results:

[code]
$result = mysql_query("SELECT id, DATEDIFF(columnname, NOW()) AS time_in_db FROM tablename");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  if ($row['time_in_db']  > 30) {
    $ids_to_remove[] = $row['id'];
  }
}

$query = "DELETE FROM tablename WHERE id IN('" . implode("', '", $ids_to_remove) . "')";
mysql_query($query) or die(mysql_error());
[/code]

You may have to modify the DATEDIFF a little depending on if it's an actual timestamp column or not.  If not, just use DATE_FORMAT and other mysql functions.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
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.