Hey all~

Recently I set up a simple database for a "News and Updates" page. Basically it's just a comment field that is stored with a current timestamp. It's almost the way I want it but I'm having trouble getting the date to display correctly. I thought that I could assign the timestamp to a variable and use strftime() to format. Unfortunately that bit, the third line, is formatted how I want but it seems to see a zero date.

Any thoughts on how to get this to work

SMSapphire

Here is how the dates for the first three entries look:
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Date: 2008-05-10 02:21:02
$time_format= 2008-05-10 02:21:02
Fixed Date: Wed, 31 Dec, 1969 06:33PM

Date: 2008-04-09 09:30:22
$time_format= 2008-04-09 09:30:22
Fixed Date: Wed, 31 Dec, 1969 06:33PM

Date: 2008-03-13 12:25:52
$time_format= 2008-03-13 12:25:52
Fixed Date: Wed, 31 Dec, 1969 06:33PM

And here is the snippet of code that displays it:
---------------------------------------------------------------------------------------------------------------------------------------------------------------

<h3>What's going on around here lately???</h3>
<?php do { ?>
<p><strong>Date:</strong> <?php echo $row_rs_news_updates['date']; ?></p>


<p><strong>$time_format=</strong>
<?php $time_format = $row_rs_news_updates['date'];
echo $time_format;
?>

<p><strong>Fixed Date:</strong> <?php echo strftime("%a, %d %b, %Y %I:%M%p", $time_format); ?></p>

<p><?php echo nl2br($row_rs_news_updates['content']); ?> </p>
<hr />
<?php } while ($row_rs_news_updates = mysql_fetch_assoc($rs_news_updates)); ?>

---------------------------------------------------------------------------------------------------------------------------------------------------------------
  • The timestamp in strftime should not be a string date as yours appear to be, it should be a unix timestamp
    • To further elaborate if you use a time stamp column in MySQL its going to convert whatever you put in there to its timestamp format... If its an int it will assume its unix time, if its a string it will try its best to convert. So when you pull back out of the db youre going to have something that looks like this: 2007-01-27 13:33:37
      • Okay,

        So I guess I didn't ask the question correctly. What I want to do is take the timestamp out of the database and change it from this:

        2008-05-10 02:21:02

        to this:

        Sat, 10 May, 2008 02:21AM

        without going through the datastamp character-by-character and reformatting it all. Within the do loop I posted originally, the variable I am working with is:

        $row_rs_news_updates['date']

        which from the first two responses is a string variable. So how does one work with that?

        Thanks

        SMSapphire
  • I got it to work!! I was digging around on the net and found someone who did a similar thing to what I was trying and here's what I ended up with:

    <?php do {
    $datetime = $row_rs_news_updates['date'];
    $year = substr($datetime, 0, 4);
    $month = substr($datetime, 5, 2);
    $day = substr($datetime, 8, 2);
    $hour = substr($datetime, 11, 2);
    $minute = substr($datetime, 14, 2);
    $varTheDate = date('M d, Y, g:ia', mktime($hour, $minute, 0, $month, $day, $year));
    ?>

    <p><strong>Date: </strong>
    <?php echo $varTheDate; ?>


    <p><?php echo nl2br($row_rs_news_updates['content']); ?> </p>
    <hr />
    <?php } while ($row_rs_news_updates = mysql_fetch_assoc($rs_news_updates)); ?>
    <p> </p>

    I haven't gone through and cleaned the code up at all at this point, but at least it works!

    Thanks for the responses! At the very least, I learned that I need to dig into PHP variable types a bit more.

    SMSapphire

Recent topics in "PHP Developers"

Topic Author Replies Last Post
using dreamweavers PHP Jivatma 2 August 4, 2008
phpews Satinder 0 June 4, 2008
how works with joomla ??? iShaun 1 May 22, 2008
advice? tristram 3 March 20, 2008
Great PHP tutorial site Nunya 0 March 5, 2008