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
https://forums.phpfreaks.com/topic/135701-solved-date-format/
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707040
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707055
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707058
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707064
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707066
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707069
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
https://forums.phpfreaks.com/topic/135701-solved-date-format/#findComment-707075
Share on other sites

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.