Jump to content

PHP Date Format


centenial

Recommended Posts

Hi,

I have a database table with several hundred records. The DB table has these fields:
- id
- name
- date

All the dates are in YYYY-MM-DD format. However, when they're displayed on my website, I want the dates to display like this:

Jan 1, 2006 or
Dec 12, 2006 (whatever the correct date is)

Is there a function in PHP that I can use to convert YYYY-MM-DD to the more elegant format above?

Thanks for your help,
Link to comment
Share on other sites

first off you should change your date field name to rdate or something other than date. Can cause problems down the line.
second, what format is your date field. Is it actually a date field or a timestamp or just a string.

Look up the date() function.

[code]$row_date = date("M j, Y", strtotime("date field here"));[/code]

That will work with actual date fields. If it is a timestamp you will have to use mktime to get the correct date.

[code]<?php
$timestamp = "20061212020250";
$row_date = date("M j, Y", mktime(0, 0, 0, substr($timestamp, 4, 2), substr($timestamp, 6, 2), substr($timestamp, 0, 4)));
?>[/code]

It all depends on how you are storing your dates

Ray
Link to comment
Share on other sites

The field should be in an array of some kind. Post your code for your query and will help.

should be something like this

[code]<?php
$sql = "SELECT * FROM table_name":
$res = mysql_query($sql) or die (mysql_error());
  while($r=mysql_fetch_assoc($res)){
  $row_date = date("M j, Y", strtotime($r['last_login']));
  echo "$row_date<br>";
  }
?>[/code]

Ray
Link to comment
Share on other sites

Absolutely

[code]<?php
$sql = "SELECT DATE_FORMAT(last_login, '%b %c, %Y') AS lastlogin FROM myTable";
$res = mysql_query($sql) or die (mysql_error());
  while($r=mysql_fetch_assoc($res)){
  echo $r['lastlogin']."<br>";
  }
?>[/code]

That will give you the format you want.

Ray
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.