Jump to content

[SOLVED] Read DATETIME from mysql


daveh33

Recommended Posts

<?php
list($date $time) = explode(' ', $row['time']);
?>

$time will now hold just the time, whereas date will have day, month and year

 

I tried this but got this error: -

 

 

Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ')' in chat.php on line 65 - list($date $time) ....

 

Link to comment
Share on other sites

OK great that works - is there a way to extend that with the same method so $time can be split into hours, minutes, and seconds?

 

I am trying to set up an if statement where it displays entries from database within past 5 minutes.

 

How is this possible?

Link to comment
Share on other sites

OK - how could I integrate that with my query:

 

$query = mysql_query("SELECT * FROM messages WHERE roomid='$rid' && `to`='all' ORDER BY `time`") or die (mysql_error());

 

so that it only gets messages from within 5 minutes ago from $today which is the current datetime

 

 

Link to comment
Share on other sites

ah, forget about exploding it all then.

If you convert the current time +5 minutes to unix:

$time = strtotime($time); //$time is a timestamp set five minutes before the current time
$query = mysql_query("SELECT * FROM messages WHERE roomid='$rid' && `to`='all' && UNIX_TIMESTAMP(time) < '$time' ORDER BY `time`") or die (mysql_error());

Link to comment
Share on other sites

 else {
$query22 = mysql_query("SELECT * FROM messages WHERE roomid='$rid' && `to`='all'") or die(mysql_error());
$row22 = mysql_fetch_array( $query22 );
$row22['time'];
$time = strtotime($time); //$time is a timestamp set five minutes before the current time
$query = mysql_query("SELECT * FROM messages WHERE roomid='$rid' && `to`='all' && UNIX_TIMESTAMP(time) < '$time' ORDER BY `time`") or die (mysql_error());
while($row = mysql_fetch_array( $query )) {
$to = $row['to'];
$id = $row['id'];
$from = $row['from'];
$message = $row['message'];
$username = $_SESSION['username'];
if ($from==$username) {
$prestyle = "<FONT COLOR=\"#0000FF\">";
$endstyle = "</font>";
}

 

Can you tell me where I am going wrong in my code using this

Link to comment
Share on other sites

at the very top of your page, just after the <?php, add a header like this:

<?php
header( 'refresh: 30;' );

 

That will refresh every 30 seconds.

 

Something I've got to refresh a page every 10 minutes based on a variable is this:

$rtime = '10';
header('refresh: ' . $rtime*60);

It needs to be multiplied by 60 to put it into seconds, otherwise it would refresh after 10 seconds, not minutes.

Link to comment
Share on other sites

Thanks that refresh works

 

just tested with the time on the mysql query - its showing messages from over 5  minutes ago - the code is

 

$time = strtotime("+5 minutes");
$query = mysql_query("SELECT * FROM messages WHERE roomid='$rid' && `to`='all' && UNIX_TIMESTAMP(time) < '$time' ORDER BY `time`") or die (mysql_error());
while($row = mysql_fetch_array( $query )) {
$to = $row['to'];
$id = $row['id'];
$from = $row['from'];
$time2 = $row['time'];
$message = $row['message'];
$username = $_SESSION['username'];
if ($from==$username) {
$prestyle = "<FONT COLOR=\"#0000FF\">";
$endstyle = "</font>";
echo $prestyle;
}
list($date2, $time2) = explode(' ', $row['time']);
echo "<b><a href=\"viewmembers.php?id=$from\">$from</a></b><small>-$time2</small>>>          <i>$message</i>$endstyle<p> <p>";
}

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.