Hindi movies, hindi songs - ID badge templates - Debt Consolidation - Wordpress Themes - Debt Consolidation

PDA

View Full Version : Convert MYSQL timestamp into Readable PHP


meganerd
Aug 9th 2006, 11:23 am
I have a script that will insert a timestamp into the database with each new submission. However, when I call on the timestamp:

<? echo $date; ?>

I get it as MYSQL sees it:

20060809141836

How can I convert this into:

August 9th, 2006

I don't care about the time part.

Or is there a better way I could be doing this rather than using the timestamp?

mad4
Aug 9th 2006, 11:28 am
http://www.php.net/date should help, post back if it doesn't. :)

meganerd
Aug 9th 2006, 11:36 am
http://www.php.net/date should help, post back if it doesn't. :)

Darn it! I was trying to be lazy and you ruined it. :p Thanks.

Just kidding.

I'm a visual learner and when it comes to PHP, I learn backwards. I've tried for 5 years to read php.net and other help sites but it never sank in until I get the actual code. Only then could I learn how php works by dissecting the code and basically trial and error.

However, I'm not using that as an excuse. I'm going to go to the url you suggested and see what I can come up with. Thanks!

meganerd
Aug 9th 2006, 12:04 pm
Yay! I found something to replace it.

I changed date to varchar instead of timestamp. And then I used:

<input type="hidden" name="date" value="<? echo date('F j, Y'); ?>">

on the form to auto come up with the date for me. And Wahoo! It's works. Thanks.

falcondriver
Aug 9th 2006, 12:08 pm
if you get this timestamp from mysql, it is probably faster and better if you use the mysql date function, so you can just use the string in php:

SELECT DATE_FORMAT(mytimestamp, '%M %D, %Y') AS mydate FROM mytable;

see also http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html ands scroll down a few pages.

ip076
Aug 9th 2006, 4:40 pm
I would say FalconDriver has the best idea. If you for some reason cannot do that, I would use a combination of php's date and strtotime functions.


$NewDate = date("F jS, Y", strtotime($MysqlDate));


Both of these I think are better options than storing the information from a hidden form variable. I could spoof your form and include anything in that field since you made it a varchar field.

Use the built-in MySQL Date Functions...if not possible, use the PHP functions...

webviz
Aug 9th 2006, 9:24 pm
$date = date("F jS, Y", $timestamp_stored_in_db);

ip076
Aug 10th 2006, 5:03 am
$date = date("F jS, Y", $timestamp_stored_in_db);

I'm not sure, will this work? The MySQL date given is more of a String Date than a Unix Timestamp, thats why I used strtotime.

neebski
Sep 21st 2007, 4:05 pm
<? echo $date = date("F js, Y", $row_logs['time']; ?>

Eh? I cant seem to get it work on my end. Thoughts?

falcondriver
Sep 22nd 2007, 6:51 am
try <? echo date("F js, Y", $row_logs['time']; ?>