• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

GET the DATE and replace for text

I need to replace the dates with this format (year/month/day) in text like: September 2013

Here is an extract of the code:

mysql_data_seek ($result, 0);
	while ($row = mysql_fetch_assoc ($result)) {
   //get the publisher name
  $organizerSql = mysql_query("SELECT nazwa FROM baza_obiektow_inne WHERE id=" . $row['organizer_id'] . " LIMIT 1");
   $organizer = mysql_fetch_assoc($organizerSql);

   echo "<tr>\n";
   printf("<td><a href='http://embs-group.com/%s,%s'>%s</a></td>", $row['id'], str_replace(" ", "_", $row['name']), $row['name']);
   printf("<td>%s</td>", $organizer['nazwa']);
   printf("<td>%s</td>", $row['no_pages']);
   printf("<td>%s</td>", $row['publication_date']); // Here is where I get the date 
   printf("<td>%s</td>", $row['price']);
   printf("<td>%s</td>", $row['currency']);
   echo "</tr>\n";

Open in new window

Klaus Andersen
Klaus Andersen
  • 3
1 Solution
Marco GasiFreelancerCommented:
$year = substr($row['publication_date'], 0, 4);
$month = substr($row['publication_date'], 5, 2);
switch ($month) {
  case '01':  
      $month = 'january';
  case '02':  
      $month = 'february';
  case '03':  
      $month = 'march';
  // and so on...


printf("<td>%s</td>", $month . " " . $year); // Here is where I get the date

I didn't test this but I think it should work :)

Ray PaseurCommented:
The date columns in the database should be of the type DATE or DATETIME.  They will come back to your PHP script in the form of the ISO-8601 date/time string.  You use strtotime() and date() to convert these values into "pretty dates" for public display.  Example:

$pretty_date = date('F Y', strtotime($row['publication_date']));
Klaus AndersenAuthor Commented:
Excellent Ray, that´s exactly what I needed. I was trying to get the text value for the dates from a table I created specially for that. But your solution is much smarter and simply. Many thanks!
Ray PaseurCommented:
Thanks for the points.  The advantage of using the built-in PHP functions (over the text string manipulation) begins to be obvious when you need to start doing arithmetic with dates, like "Next Wednesday."
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now