Jump to content

[SOLVED] Date Formats when calling a date from a database


gazfocus

Recommended Posts

I am designing a website that will have event dates on it which will be stored in a database. I am in the early stages of creating the page to call the info from the database so please excuse the basicness of the code, but my question is...

 

When the page pulls the date from the database it's in yyyy-mm-dd format but I would like it to be displayed as (for example) 1st January 2009.

 

Is this possible?

 

Here is my code:

<html>
<head>
<SCRIPT TYPE="text/javascript">
</SCRIPT>
<link href="/joomla/templates/at_flexmen/css/template_css.css" rel="stylesheet" type="text/css">
</head>
<body>

<?php

//////////////////////////////////////////
//// MySQL Database Connection ///////////
//////////////////////////////////////////
$host = "xxxxx";
$user = "xxxxx";
$db_name= "xxxxx";
$pass= "xxxxx";

$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(mysql_error());

$sql = "SELECT * FROM kirkbydiarydates" or die(mysql_error());
$result=mysql_query($sql,$conn);

while ($newArray = mysql_fetch_array($result))
	{
	$eventTime = $newArray[eventTime];
	$eventDate = $newArray[eventDate];
	$venue = $newArray[venue];
	$title = $newArray[title];
	$description = $newArray[description];
	}

echo $eventTime . "<br />";
echo $eventDate . "<br />";
echo $venue . "<br />";
echo $title . "<br />";
echo $description . "<br />";


?>
</body>
</html>

 

 

Link to comment
Share on other sites

Ok. For starters, SELECT * is very inefficient, and your code needs some definite attention.

 

How many records are you expecting your query to retrieve? Because at the moment it will only ever display the last one.

As you can probably tell, I am a beginner at MySQL... I will be refining my query to fetch all events where the date is equal to or after the current date, so I guess it could be upto a 20-30 records.

Link to comment
Share on other sites

Use the date() function to format a timestring from a database.

 

You'll also want to put it into a function. It may not seem necessary now, but when/if you have multiple pages that use the date() function and you decide you want to change the format, you'll have to change it on every page. If you put it in a function, you only have to change it once.

 

Example

<?php
function display_time_string($timestamp){
	if($timestamp==0) return 'never';
	return date('M j Y', $timestamp);
}
//$timestamp is the timestamp from the database
?>

Link to comment
Share on other sites

Use the date() function to format a timestring from a database.

 

You'll also want to put it into a function. It may not seem necessary now, but when/if you have multiple pages that use the date() function and you decide you want to change the format, you'll have to change it on every page. If you put it in a function, you only have to change it once.

 

Example

<?php
function display_time_string($timestamp){
	if($timestamp==0) return 'never';
	return date('M j Y', $timestamp);
}
//$timestamp is the timestamp from the database
?>

 

Thanks for that. For the 'eventTime' in my database, it's a VARCHAR because some people that enter the data in the old site tend to put just '7pm' rather than 19:00, etc. The 'eventDate' is a DATE field. So in your example, do I still use timestring or something else?

Link to comment
Share on other sites

Id'e still recomend retrieving the date in the format you actually want.

 

You'll need to clean your code up a bit too to handle multiple records and not throw warnings.

 

<html>
<head>
<SCRIPT TYPE="text/javascript">
</SCRIPT>
<link href="/joomla/templates/at_flexmen/css/template_css.css" rel="stylesheet" type="text/css">
</head>
<body>

<?php

$host = "xxxxx";
$user = "xxxxx";
$db_name= "xxxxx";
$pass= "xxxxx";

$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(mysql_error());

$sql = "SELECT eventTime, DATE_FORMAT(eventDate,'M j y') as eDate , venue, title, description FROM kirkbydiarydates";
if ($result = mysql_query($sql,$conn)) {
  if (mysql_num_rows($result)) {
    while ($newArray = mysql_fetch_array($result)) {
      $eventTime = $newArray['eventTime'];
      $eventDate = $newArray['eDate'];
      $venue = $newArray['venue'];
      $title = $newArray['title'];
      $description = $newArray['description'];

      echo $eventTime . "<br />";
      echo $eventDate . "<br />";
      echo $venue . "<br />";
      echo $title . "<br />";
      echo $description . "<br />";
    }
  }
}

?>
</body>
</html>

Link to comment
Share on other sites

Id'e still recomend retrieving the date in the format you actually want.

 

You'll need to clean your code up a bit too to handle multiple records and not throw warnings.

 

 

Thanks for your help thorpe, i really appreciate it. I copied your code in and I do receive all the records which is great, but instead of getting a date, i get "M j y" (minus the ")

Link to comment
Share on other sites

That's because you store your dates in a varchar-type field (which I assume they are already formatted). You should use an int-type field, instead and store your dates/times as 10-digit integers. To convert your existing dates, use the strtotime() php function (go to PHP.net and search for it).

Link to comment
Share on other sites

Sorry, your query should be.....

 

$sql = "SELECT eventTime, DATE_FORMAT(eventDate,'%D %M %Y') as eDate , venue, title, description FROM kirkbydiarydates";

 

A huge thank you to you. I have also (from your code) figured out how to add the day too so my result now reads Sunday 4th January 2009.

 

Many thanks again

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.