Jump to content

[SOLVED] Date Format


toxictoad

Recommended Posts

Hi, I've been searching for a way to format the date pulled from my database and I've found a lot of different info but I just can't understand how I adapt what I've found to fit into my current page/code?

 

Like this thread: http://www.phpfreaks.com/forums/index.php/topic,123000.0.html and other refer to the strtotime and I found another that gives me the correct format

$insert[review_date] = date("j F, Y",strtotime($insert[review_date]));

will change it from 30/08/2005 to 30 August, 2005 

 

So it looks like the format I want is "j F, Y"

 

But how do I adapt it to fit into my code?

<?php
require('config.php');

$id = $_GET['id'];
$sql = "SELECT * FROM rssfeeds WHERE NewsID = '$id'";
$res = mysql_query($sql) or die(mysql_error());

while($row=mysql_fetch_array($res)){
        $title=$row['Title'];
        $pubdate=$row['PubDate'];
        $description=$row['Description'];
        $content=$row['Content'];
        $source=$row['Source'];
}
?>

Published: <?=$pubdate?>

 

Thanks

Link to comment
Share on other sites

i think this is what you're looking for

 

while($row=mysql_fetch_array($res)){
        $title=$row['Title'];
        $pubdate=date("j F, Y",strtotime($row['PubDate']));
        $description=$row['Description'];
        $content=$row['Content'];
        $source=$row['Source'];
}

Link to comment
Share on other sites

fetch_array gives you options as to how the array is stored (associate, numeric or both) where as fetch_array just returns the assoc

 

so;

 

$result = mysql_query("SELECT id, name FROM table");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "ID: {$row['id']}  Name: {$row['id']}");
}

 

see how fetch_array can be set, the other options are MYSQL_NUM and MYSQL_BOTH

Link to comment
Share on other sites

Thanks all.

 

$pubdate = date("j F, Y",strtotime($pubdate));

Gave me a wrong date

 

but

$pubdate=date("j F, Y",strtotime($row['PubDate']));

works perfect :)

 

As for why the use of array() over assoc() I don't know, I'm really new to PHP and I'm just trying to piece things together. Both work (just tested) so someone with more PHP experience should be able to explain the difference I guess?

 

Thanks again!

Link to comment
Share on other sites

Thanks all.

 

$pubdate = date("j F, Y",strtotime($pubdate));

Gave me a wrong date

 

but

$pubdate=date("j F, Y",strtotime($row['PubDate']));

works perfect :)

 

As for why the use of array() over assoc() I don't know, I'm really new to PHP and I'm just trying to piece things together. Both work (just tested) so someone with more PHP experience should be able to explain the difference I guess?

 

Thanks again!

 

The one that didn't work didn't work because you put it on the wrong place :)

Link to comment
Share on other sites

Thanks all.

 

$pubdate = date("j F, Y",strtotime($pubdate));

Gave me a wrong date

 

but

$pubdate=date("j F, Y",strtotime($row['PubDate']));

works perfect :)

 

As for why the use of array() over assoc() I don't know, I'm really new to PHP and I'm just trying to piece things together. Both work (just tested) so someone with more PHP experience should be able to explain the difference I guess?

 

Thanks again!

 

The one that didn't work didn't work because you put it on the wrong place :)

 

Yeah, he probably put it in the while loop.  Anyway, they are both pretty much the same thing, and both should work.

Link to comment
Share on other sites

Thanks all.

 

$pubdate = date("j F, Y",strtotime($pubdate));

Gave me a wrong date

 

but

$pubdate=date("j F, Y",strtotime($row['PubDate']));

works perfect :)

 

As for why the use of array() over assoc() I don't know, I'm really new to PHP and I'm just trying to piece things together. Both work (just tested) so someone with more PHP experience should be able to explain the difference I guess?

 

Thanks again!

 

The one that didn't work didn't work because you put it on the wrong place :)

 

Yeah, he probably put it in the while loop.  Anyway, they are both pretty much the same thing, and both should work.

 

I see...I did put it in the 'while loop' Doh!

 

Link to comment
Share on other sites

Thanks all.

 

$pubdate = date("j F, Y",strtotime($pubdate));

Gave me a wrong date

 

but

$pubdate=date("j F, Y",strtotime($row['PubDate']));

works perfect :)

 

As for why the use of array() over assoc() I don't know, I'm really new to PHP and I'm just trying to piece things together. Both work (just tested) so someone with more PHP experience should be able to explain the difference I guess?

 

Thanks again!

 

The one that didn't work didn't work because you put it on the wrong place :)

 

Yeah, he probably put it in the while loop.  Anyway, they are both pretty much the same thing, and both should work.

 

I see...I did put it in the 'while loop' Doh!

 

 

Lol!  This is what I meant...

 

while($row=mysql_fetch_array($res)){
        $title=$row['Title'];
        $pubdate=$row['PubDate'];
        $description=$row['Description'];
        $content=$row['Content'];
        $source=$row['Source'];
}

$pubdate=date("j F, Y",strtotime($pubdate));
echo "Published" . $pubdate;

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.