zhangy Posted May 31, 2009 Share Posted May 31, 2009 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!"; } ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Why did you use strtotime on a date string after you formatted it with date? strtotime returns false in your case, so empty of false is true. If you want to do math, don't format the string. Instead, you can do: $date = intval($row['date']); Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 Because the date is entered into the database using time(). So i did that in order to format it. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 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. Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 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. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 So if you don't understand, how would I understand? ??? Have you even tried my suggestion a few posts up? Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 Yes, I tried and got the message "No date provided". :'( Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Show me the updated code please. Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 <?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!"; } ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Change: $unix_date = strtotime($date); To $unix_date = $date; Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 Ah, still get "no date provided". I don't understand it. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Hmm... change: $date = intval($row['date']); To var_dump($row['date']); $date = intval($row['date']); Tell me what that outputs. Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 Outputs the same... "No date provided". Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 --__-- What did var_dump output? Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 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)) { Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Once again, does var_dump line output anything? Quote Link to comment Share on other sites More sharing options...
zhangy Posted May 31, 2009 Author Share Posted May 31, 2009 It outputs things like the following: string(10) "1243786157" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.