iamnottheend Posted March 22, 2007 Share Posted March 22, 2007 I save the time of a blog post using NOW(). The db column is formatted as postdate. The time comes out as such.. 2007-03-15 15:35:41 I want to format this as day/month/year. I have tried $postdate = $row['blog_postdate']; $blog_postdate = date(d-m-Y,$postdate); but i just can't get it going. Am i even using the right fuction? Link to comment https://forums.phpfreaks.com/topic/43839-date/ Share on other sites More sharing options...
mjlogan Posted March 22, 2007 Share Posted March 22, 2007 www.php.net/strtotime is your friend here. $postdate = strtotime($row['blog_postdate']); $blog_postdate = date("d-m-Y",$postdate); Link to comment https://forums.phpfreaks.com/topic/43839-date/#findComment-212808 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2007 Share Posted March 22, 2007 The second parameter to the date() function needs to be a UNIX timestamp, not an ASCII string. You need to use the strtotime() function to convert the string into a UNIX timestamp: <?php $postdate = $row['blog_postdate']; $blog_postdate = date(d-m-Y,strtotime($postdate)); ?> or just <?php $blog_postdate = date(d-m-Y,strtotime($row['blog_postdate'])); ?> Ken Link to comment https://forums.phpfreaks.com/topic/43839-date/#findComment-212810 Share on other sites More sharing options...
DeathStar Posted March 22, 2007 Share Posted March 22, 2007 or $la=time()-$r['$postdate']; $unit="seconds"; if($la >= 60) { $la=(int) ($la/60); $unit="minutes"; } if($la >= 60) { $la=(int) ($la/60); $unit="hours"; if($la >= 24) { $la=(int) ($la/24); $unit="days"; } } echo "Posted: $la $unit ago"; Link to comment https://forums.phpfreaks.com/topic/43839-date/#findComment-212813 Share on other sites More sharing options...
obsidian Posted March 22, 2007 Share Posted March 22, 2007 OR... just pull it from MySQL in the format you need: SELECT DATE_FORMAT(blog_postdate, "%d/%m/%Y") AS formattedDate FROM myTable; See the MySQL Date and Time Functions page for more options. Link to comment https://forums.phpfreaks.com/topic/43839-date/#findComment-212815 Share on other sites More sharing options...
iamnottheend Posted March 22, 2007 Author Share Posted March 22, 2007 Thanks for the help guys. I came up with the first example of what ken had after mjlogan refered me to strtotime. However it only is returning 0 and not the time. New Code.. $sql="SELECT * FROM blog LIMIT 5"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_assoc ($result)) { $blog_id[] = $row['blog_id']; $blog_title[] = $row['blog_title']; $blog_meta = $row['blog_meta']; $blog_content[] = $row['blog_content']; $blog_postdate[] = date(d-m-Y,strtotime($row['blog_postdate'])); $smarty->assign("blog_id", $blog_id); $smarty->assign("blog_title", $blog_title); $smarty->assign("blog_meta", $blog_meta); $smarty->assign("blog_content", $blog_content); $smarty->assign("blog_postdate", $blog_postdate); $smarty->assign("blog_author", "Jason"); $singlearticle = TRUE; } HTML {section name=blog loop=$blog_title} <div class="blog_left"> <h4>{$blog_title[blog]}</h4> {$blog_content[blog]} <div class="left"> Posted by {$blog_author} </div> <div class="right"> {$blog_postdate[blog]} </div> </div> {/section} Link to comment https://forums.phpfreaks.com/topic/43839-date/#findComment-212816 Share on other sites More sharing options...
mjlogan Posted March 22, 2007 Share Posted March 22, 2007 $blog_postdate[] = date(d-m-Y,strtotime($row['blog_postdate'])); should be $blog_postdate[] = date("d-m-Y",strtotime($row['blog_postdate'])); Link to comment https://forums.phpfreaks.com/topic/43839-date/#findComment-212817 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.