Forum Moderators: coopster
[dev.mysql.com...]
You can compute it by comparing the current timestamp minus the amount of seconds for the time difference. For example 7200 is two hours. So if you get a number the current time - 2 hours, and the update_time is greater, then you are within the two hour window.
if($row['Update_time'] > (time() - 7200))
echo "Less than 2 hours ago.";
For example, someone makes a blog post, and that goes into the mysql table with the time of the post.
So id need to find the difference between that time and the current time. I dont see how splitting it up makes it any easier, when i cant find the difference of the whole time to begin with
Format your time like, WeekDay:Month:Hour:Minute:Second:Year
Make sure each value is NUMERICAL. Name this $OldTime
split(":", $OldTime);
Now you have an array. Do this same thing with $TimeNow and subtract each array entry from its pair....then piece the results back together, converting back to a non-numerical format....see?
SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(MAX(lastupdated)) FROM foo;
...
$last = fetch...
if ($last > 24*3600) {
$string = floor($last/(24*3600)) . " days ago";
} else if ($last > 3600) {
$string = floor($last/3600) . " hours ago";
) else...
Make sense?
...and since i need this for the 5 most recent entries, will it still work, or is it just for the latest entry?
Also, how do i work it into my existing query, since i need to use it there anyways?
argh this just seems impossible.. Rational thought would dictate that something so basic as to say "this so-and-so was updates x long ago" would be easy.. This has to be the most infuriatingly fustrating experiance i've ever had with php.. I just can't do it :(
</whining>
$result7=mysql_query("SELECT DISTINCT blog_id FROM blog_entries ORDER BY entry_id DESC LIMIT 5");
$journal = array();
while ($i = mysql_fetch_array($result7)) { $last = mysql_query("SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(MAX(entry_time)) FROM blog_entries") or die(mysql_error());
if ($last > 24*3600) {
$string = floor($last/(24*3600)) . " days ago";
} else if ($last > 3600) {
$string = floor($last/3600) . " hours ago";
}
$journal[] = '<td style="padding: 10px; border: 1px solid #0056a1; vertical-align: top; background: url(\'bluegrad.gif\') repeat-x;"><center><b><a href="http://www.example.com/profiles/'.$i['blog_id'].'">'.$i['blog_id'].'</a></b><br/><br/><a href="http://www.example.com/users/'.$i['blog_id'].'">'.$mainpic.'</a><div style="color: red; font-size: 9px;">'.$string.'</div></center></td><td width="5px"></td>';
}
echo $journal[0];
echo $journal[1];
echo $journal[2];
echo $journal[3];
echo $journal[4];
if ($last > 24*3600) {
$string = floor($last/(24*3600)) . " days ago";
} else if ($last > 3600) {
$string = floor($last/3600) . " hours ago";
}
It will display nothing for any of the last 5 entries, even though most of them were made many hours ago. But if i add an "else" at the end to test it like this
if ($last > 24*3600) {
$string = floor($last/(24*3600)) . " days ago";
} else if ($last > 3600) {
$string = floor($last/3600) . " hours ago";
} else {
$sring = "none of the above is true";
}
Or something, it will say that for each entry.. The other problem is i dont know how to get it down to minutes and seconds.
1) Subtract the timestamp (time of post) from the current time to give you the number of seconds that have elapsed since the post was made. Call this $elapsed_time
2) Divide $elapsed_time by the number of seconds in a month / week / day and if the floor() result is more than 1, display a message:
// Lets say that the post is 2h 5m old
$elapsed_time = 7500;
// There are 604800 seconds in a week
$1_week = 604800;$weeks = floor($elapsed_time / $1_week);
if($weeks ==1)
{
echo 'One week';
}
elseif ($weeks > 1)
{
echo "$weeks weeks";
}
// If $week = 0 then nothing is printed.
Then do the same for days and minutes.
I hope that is what you were trying to achieve and I have not completely misinterpreted ;)
$now = time();
$then = $i['entry_time'];
$elapsed_time = $now - $then;
if ($elapsed_time > 604799) {
$one_week = '604800';
$weeks = floor($elapsed_time / $one_week);
if($weeks == 1)
{
$time = 'One week';
}
elseif ($weeks > 1)
{
$time = "$weeks weeks";
}
} else if ($elapsed_time > 86399){
$one_day = '86400';
$days = floor($elapsed_time / $one_day);
if($days == 1)
{
$time = 'One day';
}
elseif ($days > 1)
{
$time = "$days days";
}
} else if ($elapsed_time > 3599) {
$one_hour = '3600';
$hours = floor($elapsed_time / $one_hour);
if($hours == 1)
{
$time = 'One hour';
}
elseif ($hours > 1)
{
$time = "$hours hours";
}
} else if ($elapsed_time > 60){
$one_minute = '60';
$minutes = floor($elapsed_time / $one_minute);
if($minutes == 1)
{
$time = 'One minute';
}
elseif ($minutes > 1)
{
$time = "$minutes minutes";
}
} else {
$one_second = '1';
$seconds = floor($elapsed_time / $one_second);
if($seconds == 1)
{
$time = 'Right now';
}
elseif ($seconds > 1)
{
$time = "$seconds seconds";
}
Thanks again to everyone who helped me with this
function spitback_timestring($elapsed_time)
{
$num_weeks = floor($elapsed_time/604800);
$time_left = $elapsed_time - ($num_weeks*604800);
$num_days = floor($time_left/86400);
$time_left = $time_left - ($num_days*86400);
$num_hours = floor($time_left/3600);
$time_left = $time_left - ($num_hours*3600);
$num_minutes = floor($time_left/60);
$time_left = $time_left - ($num_minutes*60);
$num_seconds = $time_left;
// and then to print...
echo "$num_weeks weeks<br>
$num_days days<br>
$num_hours hours<br>
$num_minutes minutes<br>
$num_seconds seconds<br>";
}// end function
You could use the ifs to print or not print - depending - I suppose. I tried this briefly and it seems to work. I used something similar a while back.