Time

loktoy

Established
bakit po ka di nag babago yun minutes o oras ko..
lagi lang "A few second ago"

<?php
$days = floor($post_row['TimeSpent'] / (60*60*24));
$remainder = $post_row['TimeSpent'] % (60*60*24);
$hours = floor($remainder / (60*60 ));
$remainder = $remainder % (60*60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if ($days > 0)
echo date ('F d, Y - H:i:sa', $post_row['date_created']);
elseif($days == 0 && $hours ==0 && $minutes == 0) echo "A few seconds ago";
elseif ($days == 0 && $hours == 0)
echo $minutes.' minutes ago';
} ?>
 
Based on the code you provided, it seems that you are trying to display the time elapsed since a certain event occurred. However, you mentioned that the minutes and hours are not changing and are always displaying "A few seconds ago".

The issue lies in the logic of your code. You are calculating the time elapsed correctly using the $post_row['TimeSpent'] variable, but then you are not using this calculated time in your conditional statements.

To fix this issue, you need to compare the calculated time with the current time to determine how long ago the event occurred. Here's an updated version of your code:

PHP:
<?php 
$days = floor($post_row['TimeSpent'] / (60*60*24));
$remainder = $post_row['TimeSpent'] % (60*60*24);
$hours = floor($remainder / (60*60));
$remainder = $remainder % (60*60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;

$current_time = time(); // Get the current time

$time_elapsed = $current_time - $post_row['date_created']; // Calculate the time elapsed

if ($days > 0) {
    echo date('F d, Y - H:i:sa', $post_row['date_created']);
} elseif ($time_elapsed < 60) {
    echo "A few seconds ago";
} elseif ($time_elapsed < 3600) { // Less than 1 hour
    echo floor($time_elapsed / 60) . ' minutes ago';
} else {
    // Handle other cases, such as hours, days, etc.
}
?>

In the updated code, we calculate the current time using time() and subtract the $post_row['date_created'] to get the time elapsed. Then, we use conditional statements to check the elapsed time and display the appropriate message.

Make sure to adjust the code to fit your specific requirements, such as handling cases for hours, days, etc.
 
Based on the code you provided, it seems that you are trying to display the time elapsed since a certain event occurred. However, you mentioned that the minutes and hours are not changing and are always displaying "A few seconds ago".

The issue lies in the logic of your code. You are calculating the time elapsed correctly using the $post_row['TimeSpent'] variable, but then you are not using this calculated time in your conditional statements.

To fix this issue, you need to compare the calculated time with the current time to determine how long ago the event occurred. Here's an updated version of your code:

PHP:
<?php
$days = floor($post_row['TimeSpent'] / (60*60*24));
$remainder = $post_row['TimeSpent'] % (60*60*24);
$hours = floor($remainder / (60*60));
$remainder = $remainder % (60*60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;

$current_time = time(); // Get the current time

$time_elapsed = $current_time - $post_row['date_created']; // Calculate the time elapsed

if ($days > 0) {
    echo date('F d, Y - H:i:sa', $post_row['date_created']);
} elseif ($time_elapsed < 60) {
    echo "A few seconds ago";
} elseif ($time_elapsed < 3600) { // Less than 1 hour
    echo floor($time_elapsed / 60) . ' minutes ago';
} else {
    // Handle other cases, such as hours, days, etc.
}
?>

In the updated code, we calculate the current time using time() and subtract the $post_row['date_created'] to get the time elapsed. Then, we use conditional statements to check the elapsed time and display the appropriate message.

Make sure to adjust the code to fit your specific requirements, such as handling cases for hours, days, etc.
wala pa rin po pagbabago ganun pa rin po 3 mins na po nakakalipas
 
wala pa rin po pagbabago ganun pa rin po 3 mins na po nakakalipas
Apologies for the confusion. It appears that the issue lies in the value of the $post_row['TimeSpent'] variable. From the code, it seems that you are using this variable to represent the total time spent in seconds.

However, to calculate the time elapsed since the event occurred, you need to use the difference between the current time and the time the event happened. The $post_row['TimeSpent'] variable should represent the timestamp of the event, not the elapsed time.

Here's an updated version of your code:

PHP:
<?php
$current_time = time(); // Get the current time
$time_elapsed = $current_time - $post_row['date_created']; // Calculate the time elapsed in seconds

$days = floor($time_elapsed / (60*60*24));
$remainder = $time_elapsed % (60*60*24);
$hours = floor($remainder / (60*60));
$remainder = $remainder % (60*60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;

if ($days > 0) {
    echo date('F d, Y - H:i:sa', $post_row['date_created']);
} elseif ($time_elapsed < 60) {
    echo "A few seconds ago";
} elseif ($time_elapsed < 3600) { // Less than 1 hour
    echo $minutes . ' minutes ago';
} else {
    // Handle other cases, such as hours, days, etc.
}
?>

In this updated code, we calculate the time elapsed by subtracting the timestamp of the event ($post_row['date_created']) from the current time. We then use this calculated time to determine how long ago the event occurred.

Please ensure that you are passing the correct timestamp value for $post_row['date_created'] to accurately calculate the time elapsed.
 
Apologies for the confusion. It appears that the issue lies in the value of the $post_row['TimeSpent'] variable. From the code, it seems that you are using this variable to represent the total time spent in seconds.

However, to calculate the time elapsed since the event occurred, you need to use the difference between the current time and the time the event happened. The $post_row['TimeSpent'] variable should represent the timestamp of the event, not the elapsed time.

Here's an updated version of your code:

PHP:
<?php
$current_time = time(); // Get the current time
$time_elapsed = $current_time - $post_row['date_created']; // Calculate the time elapsed in seconds

$days = floor($time_elapsed / (60*60*24));
$remainder = $time_elapsed % (60*60*24);
$hours = floor($remainder / (60*60));
$remainder = $remainder % (60*60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;

if ($days > 0) {
    echo date('F d, Y - H:i:sa', $post_row['date_created']);
} elseif ($time_elapsed < 60) {
    echo "A few seconds ago";
} elseif ($time_elapsed < 3600) { // Less than 1 hour
    echo $minutes . ' minutes ago';
} else {
    // Handle other cases, such as hours, days, etc.
}
?>

In this updated code, we calculate the time elapsed by subtracting the timestamp of the event ($post_row['date_created']) from the current time. We then use this calculated time to determine how long ago the event occurred.

Please ensure that you are passing the correct timestamp value for $post_row['date_created'] to accurately calculate the time elapsed.
wala pa rin po pero ito buong code ko po

<?php
$query = mysqli_query($conn,"SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent from post LEFT JOIN user on user.user_id = post.user_id order by post_id DESC")or die(mysqli_error());
while($post_row=mysqli_fetch_array($query)){
$id = $post_row['post_id'];
$upid = $post_row['user_id'];
$posted_by = $post_row['firstname']." ".$post_row['lastname'];
?>
<a style="text-decoration:none; float:right;" href="deletepost.php<?php echo '?id='.$id; ?>"><button><font color="red">x</button></font></a>
<h3>Posted by: <a href="#"> <?php echo $posted_by; ?></a>
-
<?php
$days = floor($post_row['TimeSpent'] / (60 * 60 * 24));
$remainder = $post_row['TimeSpent'] % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0)
echo date('F d, Y - H:i:sa', $post_row['date_created']);
elseif($days == 0 && $hours == 0 && $minutes == 0)
echo "A few seconds ago";
elseif($days == 0 && $hours == 0)
echo $minutes.' minutes ago';
?>
 
wala pa rin po pero ito buong code ko po

<?php
$query = mysqli_query($conn,"SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent from post LEFT JOIN user on user.user_id = post.user_id order by post_id DESC")or die(mysqli_error());
while($post_row=mysqli_fetch_array($query)){
$id = $post_row['post_id'];
$upid = $post_row['user_id'];
$posted_by = $post_row['firstname']." ".$post_row['lastname'];
?>
<a style="text-decoration:none; float:right;" href="deletepost.php<?php echo '?id='.$id; ?>"><button><font color="red">x</button></font></a>
<h3>Posted by: <a href="#"> <?php echo $posted_by; ?></a>
-
<?php
$days = floor($post_row['TimeSpent'] / (60 * 60 * 24));
$remainder = $post_row['TimeSpent'] % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0)
echo date('F d, Y - H:i:sa', $post_row['date_created']);
elseif($days == 0 && $hours == 0 && $minutes == 0)
echo "A few seconds ago";
elseif($days == 0 && $hours == 0)
echo $minutes.' minutes ago';
?>
I apologize for the confusion. It seems that the issue lies in the way you are calculating the time elapsed. The variable $post_row['TimeSpent'] is incorrect and should be replaced with $time_elapsed which represents the difference between the current time and the time the post was created.

Here's the updated code:

PHP:
<?php
$query = mysqli_query($conn, "SELECT *, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post LEFT JOIN user ON user.user_id = post.user_id ORDER BY post_id DESC") or die(mysqli_error());

while ($post_row = mysqli_fetch_array($query)) {
    $id = $post_row['post_id'];
    $upid = $post_row['user_id'];
    $posted_by = $post_row['firstname'] . " " . $post_row['lastname'];
?>

    <a style="text-decoration:none; float:right;" href="deletepost.php<?php echo '?id=' . $id; ?>"><button><font color="red">x</button></font></a>
    <h3>Posted by: <a href="#"> <?php echo $posted_by; ?></a> - <?php
                                                                        $current_time = time();
                                                                        $time_elapsed = $current_time - $post_row['date_created'];

                                                                        $days = floor($time_elapsed / (60 * 60 * 24));
                                                                        $remainder = $time_elapsed % (60 * 60 * 24);
                                                                        $hours = floor($remainder / (60 * 60));
                                                                        $remainder = $remainder % (60 * 60);
                                                                        $minutes = floor($remainder / 60);
                                                                        $seconds = $remainder % 60;

                                                                        if ($days > 0) {
                                                                            echo date('F d, Y - H:i:sa', $post_row['date_created']);
                                                                        } elseif ($time_elapsed < 60) {
                                                                            echo "A few seconds ago";
                                                                        } elseif ($time_elapsed < 3600) {
                                                                            echo $minutes . ' minutes ago';
                                                                        } else {
                                                                            echo $hours . ' hours ago';
                                                                        }
                                                                        ?>

    <?php
}
?>

In this updated code, I have corrected the calculation of the time elapsed by using the $current_time variable instead of $post_row['TimeSpent']. It should now accurately display the elapsed time in seconds, minutes, or hours.

Please ensure that the $post_row['date_created'] value is a valid UNIX timestamp representing the time the post was created.
 
I apologize for the confusion. It seems that the issue lies in the way you are calculating the time elapsed. The variable $post_row['TimeSpent'] is incorrect and should be replaced with $time_elapsed which represents the difference between the current time and the time the post was created.

Here's the updated code:

PHP:
<?php
$query = mysqli_query($conn, "SELECT *, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post LEFT JOIN user ON user.user_id = post.user_id ORDER BY post_id DESC") or die(mysqli_error());

while ($post_row = mysqli_fetch_array($query)) {
    $id = $post_row['post_id'];
    $upid = $post_row['user_id'];
    $posted_by = $post_row['firstname'] . " " . $post_row['lastname'];
?>

    <a style="text-decoration:none; float:right;" href="deletepost.php<?php echo '?id=' . $id; ?>"><button><font color="red">x</button></font></a>
    <h3>Posted by: <a href="#"> <?php echo $posted_by; ?></a> - <?php
                                                                        $current_time = time();
                                                                        $time_elapsed = $current_time - $post_row['date_created'];

                                                                        $days = floor($time_elapsed / (60 * 60 * 24));
                                                                        $remainder = $time_elapsed % (60 * 60 * 24);
                                                                        $hours = floor($remainder / (60 * 60));
                                                                        $remainder = $remainder % (60 * 60);
                                                                        $minutes = floor($remainder / 60);
                                                                        $seconds = $remainder % 60;

                                                                        if ($days > 0) {
                                                                            echo date('F d, Y - H:i:sa', $post_row['date_created']);
                                                                        } elseif ($time_elapsed < 60) {
                                                                            echo "A few seconds ago";
                                                                        } elseif ($time_elapsed < 3600) {
                                                                            echo $minutes . ' minutes ago';
                                                                        } else {
                                                                            echo $hours . ' hours ago';
                                                                        }
                                                                        ?>

    <?php
}
?>

In this updated code, I have corrected the calculation of the time elapsed by using the $current_time variable instead of $post_row['TimeSpent']. It should now accurately display the elapsed time in seconds, minutes, or hours.

Please ensure that the $post_row['date_created'] value is a valid UNIX timestamp representing the time the post was created.
wala pa rin po kapag nirerefresh ko


<?php
$current_time =time();
$time_elapse = $current_time - $post_row['date_created'];
$days = floor($time_elapse / (60*60*24));
$remainder = $time_elapse% (60*60*24);
$hours = floor($remainder / (60*60 ));
$remainder = $remainder % (60*60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if ($days > 0)
echo date ('F d, Y - H:i:sa', $post_row['date_created']);
elseif($time_elapse < 60) {
echo "A few seconds ago";}
elseif ($time_elapse <3600){
echo floor($time_elapse / 60). 'minutes ago';
}else{
}

} ?>
 

About this Thread

  • 6
    Replies
  • 577
    Views
  • 1
    Participants
Last reply from:
loktoy

Online now

Members online
954
Guests online
1,433
Total visitors
2,387

Forum statistics

Threads
2,276,192
Posts
28,968,184
Members
1,231,159
Latest member
Cuong Pham
Back
Top