Jump to content

Format Date From a MySQL Table


suttercain

Recommended Posts

Hi Everyone,

 

I am working with a MySQL table that has a 'VARCHAR' column that is filled with dates. The dates in the table are formated as 1/1/1943 (January, 1 1943). All the dates are in that format and are being echoed as such.

 

Is there a way to code this so it takes those formats and echos them as either January 1, 1943 or even better January 1st, 1943. I would like it to do this for all the dates. EXP. 3/4/1980 = March 3, 1980 etc.

 

Here is my code:

 

<?php
// Perform an statndard SQL query:
$res = mysql_query("SELECT title, email, publish_date FROM comics ORDER BY title ASC") or die (mysql_error());

// Assuming $res holds the results from your query:
while ($row = mysql_fetch_assoc($res)) {
  echo "<td>$row[title]</td>\n";
  echo "<td>$row[email]</td>\n";
  echo "<td>$row[publish_date]</td>\n";
  echo "</tr>\n";
}

?>

 

Any help or suggestions? Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/40629-format-date-from-a-mysql-table/
Share on other sites

Here's a script I came up with, I don't think there is a php function that will do that for you.

 

*Edit* I just realized your format for the date is the full date I.E 1943, but you can modify it to fix that.

 

<?php

$date = "6/22/07";

$chars = preg_split('/\//', $date, -1, PREG_SPLIT_NO_EMPTY);

$array = array("1"=>January, "2"=>Febuary, "3"=>March, "4"=>April, "5"=>May, "6"=>June, "7"=>July, "8"=>August, "9"=>September, "10"=>October, "11"=>November, "12"=>December);

echo "{$array[$chars[0]]} ";

//If there are more that don't end in a th, then add them to this array.. all I thought of
$array2 = array("1"=>st, "2"=>nd, "3"=>rd, "21"=>st, "22"=>nd, "23"=>rd);

if (in_array("".$array2[$chars[1]]."", $array2)) {
   echo $chars[1];
   echo "{$array2[$chars[1]]}, "; 
}
else {
echo $chars[1];
echo "th, ";
}

if ($chars[2] < 15) {
echo "20";
echo $chars[2];
}
else {
echo "19";
echo $chars[2];
}

//This was a test variable while building
//print_r($chars);

?>

Hey Guys,

 

I tried

<?php
$newDate = date('F jS, Y', strtotime($row[publish_date]));
echo $newDate;
?>

 

And it echoed December 31st, 1969 over and over again.

 

Here is the code I used exactly:

$newDate = date('F jS, Y', strtotime($row[publish_date]));
  echo "<td>$newDate</td>\n";

 

What am I doing wrong?

 

Thanks again.

That echoed:

 

November 30th, 1999 November 30th, 1999 November 30th, 1999 November 30th, 1999 November 30th, 1999 November 30th, 1999 November 30th, 1999

 

over and over again. I don't even know where that date is coming from???

 

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.