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)); ?>
---------------------------------------------------------------------------------------------------------------------------------------------------------------
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)); ?>
---------------------------------------------------------------------------------------------------------------------------------------------------------------
-
Re: Another question about formatting date & time
Tue, May 13, 2008 - 1:38 PMThe timestamp in strftime should not be a string date as yours appear to be, it should be a unix timestamp -
-
Re: Another question about formatting date & time
Tue, May 13, 2008 - 3:16 PMTo 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
-
-
Re: Another question about formatting date & time
Thu, May 15, 2008 - 4:39 PMOkay,
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 -
-
Re: Another question about formatting date & time
Thu, May 15, 2008 - 4:47 PMthe problem is that 2008-05-10 02:21:02 is not a timestamp but a date... a timestamp is an integer representing the number of seconds past since 1st Jan 1970 IRC.
I would suggest pulling the date out of the database using the mysql function UNIX_TIMESTAMP(arguement), then formating using the php date() function -
-
Re: Another question about formatting date & time
Thu, May 15, 2008 - 4:50 PMsomething like
"SELECT UNIX_TIMESTAMP(datefield) as mytimestamp"
Then use mytimestamp in the php funtion according to the formating instructions at uk.php.net/date
-
-
-
-
-
Re: Another question about formatting date & time
Sat, June 7, 2008 - 2:45 PMI 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