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

Link to comment
Share on other sites

You probably don't understand because of one of the same reasons I don't understand: which is because i pieced the code together like a puzzle from other scripts hoping to come up with a logical solution... Obviously it hasn't worked.

Link to comment
Share on other sites

<?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!";
}
?>


Link to comment
Share on other sites

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))
{

 

 

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.