Jump to content

[SOLVED] $newsAdded = date("l, F jS, Y h:i a"); not working right


clown[NOR]

Recommended Posts

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

?>

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

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()?

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?

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.

 

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.