clown[NOR] Posted April 15, 2007 Share Posted April 15, 2007 cant figure this out... why does this $newsAdded = date("l, F jS, Y h:i a"); only output: Sunday, April 1 ??? Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/ Share on other sites More sharing options...
anthylon Posted April 15, 2007 Share Posted April 15, 2007 What result you want to make? I tested it and here is my result: Sunday, April 15th, 2007 04:51 pm. I'm using PHP version 5.1.0. Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229704 Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 example: Sunday, April 1st, 2007 04:45 pm Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229705 Share on other sites More sharing options...
kenrbnsn Posted April 15, 2007 Share Posted April 15, 2007 Please post the code you're using. Ken Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229708 Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 <?php if (!empty($_POST['submitted'])) { if (!empty($_POST['title']) && !empty($_POST['category']) && !empty($_POST['news'])) { $newsTitle = $_POST['title']; $newsCat = $_POST['category']; $newsContent = str_replace("\n", "<br>", $_POST['news']); $newsAuthor = $_COOKIE['username']; $newsAdded = date("l, F jS, Y h:i a"); $uAccessLevel = getAccessLevel(); if ($uAccessLevel <= 20) { $newsAuth = ""; } elseif ($uAccessLevel > 20) { $newsAuth = "OK"; } $error = array(); if (!mysql_connect($dbHost, $dbUser, $dbPass)) { echo "Unable to connect to database"; die(); } if (!mysql_select_db($dbName)) { echo "Unable to select database"; die(); } $query = "SELECT * FROM news"; $result = mysql_query($query); if (!$result) { echo "Could not run query from database 1"; die(); } $newsRows = mysql_num_rows($result); $newNewsID = $newsRows + 1; $query = " INSERT INTO news VALUES ('', '$newsTitle', '$newsCat', '$newsAuthor', '$newsAdded', '$newsContent', '$newsAuth', '$newNewsID')"; $result = mysql_query($query); if (!$result) { echo "Could not run query from database 2"; die(); } echo "News was succsessfully added."; } else { echo "1 or more field was empty"; } } ?> Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229712 Share on other sites More sharing options...
kenrbnsn Posted April 15, 2007 Share Posted April 15, 2007 What is the format of the field name in your database that holds the value? It really should be "datetime" and then you would do: <?php $newsAdded = date("Y-m-d g:i:s"); ?> Once it's stored like that you will be able to use the "date/time" functions in MySQL and also format the date for display. Ken Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229717 Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 well.. no i added a new news entry... and the date is 2007-04-15 05:11:55 this is the code I use to read everything <?php function showNews() { global $dbHost, $dbUser, $dbPass, $dbName; if (!mysql_connect($dbHost, $dbUser, $dbPass)) { die("Unable to connect to DB"); } if (!mysql_select_db($dbName)) { die("Unable to select DB"); } $query = "SELECT * FROM news"; $result = mysql_query($query); if (!$result) { die("Could not run query from DB"); } $dbNumRows = mysql_num_rows($result); if ($dbNumRows > 0) { while ($newsItem = mysql_fetch_assoc($result)) { echo '<table border="0" cellspacing="1" cellpadding="0" width="100%" bgcolor="#000000">'; echo '<tr><td bgcolor="#B9C6FF"><font id="headline">'.$newsItem['title'].'</font></td></tr>'; echo '<tr><td bgcolor="#FFFFFF">'.$newsItem['news'].'</td></tr>'; echo '<tr><td bgcolor="#E3E3E3"><div align="right"><em>posted '.$newsItem['added'].' by '.$newsItem['author'].'</em></div></td></tr>'; echo '</table><br>'; } } else { echo "No news found."; } } ?> how do I change that to the way I want it to be displayed? mktime()? Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229720 Share on other sites More sharing options...
anthylon Posted April 15, 2007 Share Posted April 15, 2007 Your database filed should be datetime format. Now, date should be stored in format 'Y-m-d H:i:s', Use this: $newsAdded = date('Y-m-d H:i:s'); It must work! ??? I have tested it. Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229724 Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 yes... it works... I was just looking at the wrong news entry... sorry... but I want the date format to be shown as Day, Month Date, YEAR Hour:Min AM/PM how can i convert Year-Month-Date Hour:Min:Sec into the other format? Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229727 Share on other sites More sharing options...
anthylon Posted April 15, 2007 Share Posted April 15, 2007 I'm in rush. I can't give you better answer. But maybe later. You can format your date in the sql query. But i think you should do that in PHP after you get result from db because it's easier to change format (for users from other countries and...). Here is how I implemented that for get date in format: dd.mm.yyyy and time: //Formatiranje datuma vracenog kao rezultat iz baze: godina-mjesec-dan sat:minuta:sekunda function formatDate($date) { if ($date=='') return; $date = explode(" ", $date); //dobijamo niz [0]=>datum [1]=>vrijeme $date = explode("-", $date[0]); //dobijamo niz [0]=>godina [1]=>mjesec [2]=>dan $date = "${date[2]}.${date[1]}.${date[0]}."; return $date; } //Formatiranje vremena vracenog kao rezultat iz baze: godina-mjesec-dan sat:minuta:sekunda function formatTime($time) { if ($time=='') return; $time = explode(" ", $time); //dobijamo niz [0]=>datum [1]=>vrijeme $time = explode(":", $time[1]); //dobijamo niz [0]=>godina [1]=>mjesec [2]=>dan $time = "${time[0]}:${time[1]}:${time[2]}"; return $time; } $date = $time = result from db. Sorry I'm in hurry. Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229731 Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 that was all i needed to know, just made some changes to make it fit my script... thank guys ... Link to comment https://forums.phpfreaks.com/topic/47105-solved-newsadded-datel-f-js-y-hi-a-not-working-right/#findComment-229738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.