Reply LinkBack Thread Tools Search this Thread
Old 08-11-2004, 13:49   #1 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
convert date into a viewable format

Hi all,

I am trying to display a last updated date field on my web page.

Currently I retrieve the date via this piece of code:

<p>Last Updated: <?php echo $row_rsHome['date']; ?></p>

How do I format this date to show the date as:

8th November, 2004.

Many thanks in advance
  Reply With Quote
Old 08-11-2004, 14:00   #2 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,206
how is it stored in the db? timestamp? datetime format?
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 08-11-2004, 14:09   #3 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Well I believe that timestamp is the best, so that is what it is in.
  Reply With Quote
Old 08-11-2004, 16:11   #4 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,206
Code:
<?php echo date('jS F, Y', $row_rsHome['date'];) ?>
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 08-11-2004, 19:09   #5 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
I have done that but it is returning 19th January, 2038.

BTW

Your code returned a blank page: So I changed the single quotes to double quotes and it worked fine.

Sorry my mistake, it was actually the bracket that needed to be swapped with the semi-colon.

Last edited by NickToye : 08-11-2004 at 19:46.
  Reply With Quote
Old 09-11-2004, 04:23   #6 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,206
you're welcome.
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 09-11-2004, 05:14   #7 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
It still doesn't work right though, the date is the 2038 one, any suggestions?
  Reply With Quote
Old 09-11-2004, 05:35   #8 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
Try this:

In your mysql query:

date_format(date, '%D %M %Y) as date

Then:

echo $row['date']
  Reply With Quote
Old 09-11-2004, 05:49   #9 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,206
Quote:
Originally Posted by NickToye
It still doesn't work right though, the date is the 2038 one, any suggestions?
Are you sure you're using a unix timestamp?

this works
Code:
<?php $date = 1099907234; echo date('jS F, Y', $date); ?>

http://www.lifeofsmallbeer.co.uk/tests/date.php
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 09-11-2004, 06:46   #10 (permalink)
Mik
Magazines™
 
Mik's Avatar
 
Join Date: Mar 2003
Location: Glasgow..
Posts: 11,886
Quote:
Originally Posted by NickToye
I have done that but it is returning 19th January, 2038.

BTW

Your code returned a blank page: So I changed the single quotes to double quotes and it worked fine.

Sorry my mistake, it was actually the bracket that needed to be swapped with the semi-colon.

fucks sake smallone if your going to help at least put the effort in.
  Reply With Quote
Old 09-11-2004, 16:59   #11 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Quote:
Originally Posted by Joewebber
Try this:

In your mysql query:

date_format(date, '%D %M %Y) as date

Then:

echo $row['date']

I think I'm being a little stupid here, but where can I put this mySQL code? in my php page?

This is my current code:

Code:
<?php require_once('Connections/connNickToye.php'); ?> <?php mysql_select_db($database_connNickToye, $connNickToye); $query_rsHome = "SELECT * FROM home"; $rsHome = mysql_query($query_rsHome, $connNickToye) or die(mysql_error()); $row_rsHome = mysql_fetch_assoc($rsHome); $totalRows_rsHome = mysql_num_rows($rsHome); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/main.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <!-- InstanceBeginEditable name="doctitle" --> <title>NickToye 2005</title> <!-- InstanceEndEditable --><link href="css/myStyle.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> <!-- function getWindowHeight() { var windowHeight = 0; if (typeof(window.innerHeight) == 'number') { windowHeight = window.innerHeight; } else { if (document.documentElement && document.documentElement.clientHeight) { windowHeight = document.documentElement.clientHeight; } else { if (document.body && document.body.clientHeight) { windowHeight = document.body.clientHeight; } } } return windowHeight; } function setFooter() { if (document.getElementById) { var windowHeight = getWindowHeight(); if (windowHeight > 0) { var contentHeight = document.getElementById('content').offsetHeight; var footerElement = document.getElementById('footer'); var footerHeight = footerElement.offsetHeight; if (windowHeight - (contentHeight + footerHeight) >= 0) { footerElement.style.position = 'relative'; footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px'; } else { footerElement.style.position = 'static'; } } } } window.onload = function() { setFooter(); } window.onresize = function() { setFooter(); } //--> </script> </head> <body> <div id="container"> <div id="header"></div> <!-- InstanceBeginEditable name="nav" --> <div id="nav"> <li><a href="home.php" class="active">HOME </a></li> <li><a href="about.php">ABOUT</a></li> <li><a href="blog.php">BLOG</a></li> <li><a href="work.php">WORK</a></li> <li><a href="contact.php">CONTRACT ME</a></li> </div> <!-- InstanceEndEditable --> <div id="content"><!-- InstanceBeginEditable name="main" --> <p class="title">What Am I Doing Now?</p> <p><?php echo $row_rsHome['text']; ?></p> <p>Last Updated: <?php echo date('jS F, Y', strtotime($row_rsHome['date']); ?></p> <hr size="1" noshade="noshade" /> <p>&nbsp;</p> <!-- InstanceEndEditable --></div> <div id="sidebar"> <!-- InstanceBeginEditable name="sidebar" --><span class="sidebarHeader">Recent Articles </span> <ul> <li>Link Item 1</li> <Li>Link Item 2</Li> <li>Link Item 3</li> <li>Link Item 4</li> </ul> <span class="sidebarHeader">Inspiration </span> <!-- InstanceEndEditable --></div> <div id="footer"> </div> </div> </body> <!-- InstanceEnd --></html> <?php mysql_free_result($rsHome); ?>
  Reply With Quote
Old 09-11-2004, 18:53   #12 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
You need to put it in your query:

$query_rsHome = "SELECT date_format(blah blah blah), ..other fields here.. FROM home";
  Reply With Quote
Old 09-11-2004, 19:29   #13 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Ok this is my query in my php file:

$query_rsHome = "SELECT * FROM home";

So basically how do I adapt this to what you have suggested, because when I try:

$query_rsHome = "SELECT date_format(date, %D %M % Y), * FROM home";

it gives me an error.
  Reply With Quote
Old 09-11-2004, 19:54   #14 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
Quote:
Originally Posted by NickToye
Ok this is my query in my php file:

$query_rsHome = "SELECT * FROM home";

So basically how do I adapt this to what you have suggested, because when I try:

$query_rsHome = "SELECT date_format(date, %D %M % Y), * FROM home";

it gives me an error.

you need it like this:

$query_rsHome = "SELECT date_format(date, %D %M % Y) as date, * FROM home";

But instead of the * you need to list the other fields
  Reply With Quote
Old 09-11-2004, 19:58   #15 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
SELECT date_format(date, %D %M %Y) as date, home_id, title, text
FROM home

Is that correct?

Because again it doesn't like it.
  Reply With Quote
Old 09-11-2004, 20:18   #16 (permalink)
seen.to
unusual suspect ™
 
seen.to's Avatar
 
Join Date: Jul 2004
Location: DE, USA
Posts: 3,155
Just change the date when you update the page.
__________________
  Reply With Quote
Old 09-11-2004, 20:41   #17 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
???

huh, expand
  Reply With Quote
Old 10-11-2004, 05:29   #18 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
What error are you getting?
Might be something else coz that should work
  Reply With Quote
Old 10-11-2004, 05:57   #19 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,127
I generally store my dates in mysql as native dates, then just use the following function to convert.

Code:
function sql2phpdt($datetime) { $dt = explode(' ', $datetime); $date = explode('-', $dt[0]); $time = explode(':', $dt[1]); $date = mktime ($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]); return $date; }

usage:

Code:
<? print date('jS F, Y', sql2phpdt($date)); ?>


For what you need, you won't need to go the other way, as you can just use NOW() on your insert query.
  Reply With Quote
Old 10-11-2004, 06:16   #20 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Quote:
Originally Posted by Joewebber
What error are you getting?
Might be something else coz that should work

Code:
1064 You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '%D %M %Y) as date , home_id, title, text FROM home' at line 1

This is my query:

Code:
SELECT date_format(date, %D %M %Y) as date, home_id, title, text FROM home
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8