Jump to content

[SOLVED] Need help please.


zhangy

Recommended Posts

The following code echos database information. The problem is with the date. It doesn't echo the time like it should be returns the "bad date" message. Can anyone see what is wrong with this code?

 

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
        
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    return "$difference $periods[$j] {$tense}";
}

require_once('load.php');

$sql = "SELECT * FROM $table ORDER BY sub_id";
$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);
if(mysql_num_rows($result) >= 1)
{

$date = date("Y-m-d H:i", $row['date']);
$timeago = nicetime($date); // 2 days ago


echo '<div id="front_page_post_container">';
while($row = mysql_fetch_array($result))
{
	echo '<div id="hover">';
	echo '<div id="title">';
	echo '<h2>';
	echo '<a href="post.php?id='.$row['id'].'">'.stripslashes($row['title']).'</a>';
	echo '</h2>';
	echo '</div>';
	echo '<div id="postr">';
	echo stripslashes(nl2br(substr($row['post'],0,450)));
	echo ' <a href="post.php?id='.$row['id'].'">Read on...</a>';
	echo '</div>';
	echo '<div id="date_container">';

	echo $timeago;

	echo '</div>';
	echo '</div>';
}
echo '</div>';
}
else
{
echo "That record does not exist!";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/160359-solved-need-help-please/
Share on other sites

But it doesn't make any sense. You want it in Y-m-d H:i:s format, but you don't need it in that format. All you're doing is using nicetime() to print out some text. It looks like you want to format a UNIX time in Y-m-d H:i:s format and assume strtotime will format it back to UNIX time. If you're going to do that, why format it in the first place? I'm not understanding.

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
        
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    return "$difference $periods[$j] {$tense}";
}

require_once('load.php');

$sql = "SELECT * FROM $table ORDER BY sub_id";
$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);
if(mysql_num_rows($result) >= 1)
{

$date = intval($row['date']);
$timeago = nicetime($date); // 2 days ago


echo '<div id="front_page_post_container">';
while($row = mysql_fetch_array($result))
{
	echo '<div id="hover">';
	echo '<div id="title">';
	echo '<h2>';
	echo '<a href="post.php?id='.$row['id'].'">'.stripslashes($row['title']).'</a>';
	echo '</h2>';
	echo '</div>';
	echo '<div id="postr">';
	echo stripslashes(nl2br(substr($row['post'],0,450)));
	echo ' <a href="post.php?id='.$row['id'].'">Read on...</a>';
	echo '</div>';
	echo '<div id="date_container">';

	echo $timeago;

	echo '</div>';
	echo '</div>';
}
echo '</div>';
}
else
{
echo "That record does not exist!";
}
?>


Sorry, its frustrating to me too. Especially since I'm the one causing the problem. I made the change you told me but got the same unwanted result: "No date provided"

 

<?php
//...

sql = "SELECT * FROM $table ORDER BY sub_id";
$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);
if(mysql_num_rows($result) >= 1)
{

var_dump($row['date']);
$date = intval($row['date']);
$timeago = nicetime($date); // 2 days ago


echo '<div id="front_page_post_container">';
while($row = mysql_fetch_array($result))
{

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.