View Full Version : DATE Formatting
Tim Mousel
Thu 22nd Feb '01, 3:36am
Hi,
I have a date field stored in a mysql table in this format: 2001-02-22
How do I display that date on my page in this format:
02-22-01
Thanks in advance,
Tim
JimF
Thu 22nd Feb '01, 4:00am
First initialize your date field from MySQL as the variable $date. Then insert this code on the next line:
$tempdate = explode( "-", $date );
$year = $tempdate[0];
$month = $tempdate[1];
$day = $tempdate[2];
$newdate = date('m-d-y', mktime(0,0,0,$month,$day,$year));
print("$newdate");
Hope this helps :)
-jim
Tim Mousel
Thu 22nd Feb '01, 8:24pm
Hi,
Thanks for the info.
The results that gives me is 12-31-69.
http://worldwideag.net/auction/auction.php
I'm printing the results out into a table and it's the same date for each record. Here is the code I'm using:
// format results by row
while ($row = mysql_fetch_array($sql_result)) {
$id = $row["id"];
$date = $row["date"];
$town = $row["town"];
$state = $row["state"];
$seller = $row["seller"];
$category = $row["category"];
$broker = $row["broker"];
$sellurl = $row["sellurl"];
$brokerurl = $row["brokerurl"];
$tempdate = explode( "-", $date );
$year = $tempdate[0];
$month = $tempdate[1];
$day = $tempdate[2];
$newdate = date('m-d-y', mktime(0,0,0,$year,$month,$day));
?>
<tr>
<td width="45" valign="middle" align="center"><font color="#0000A0" face="Century Gothic"><small><? echo "$newdate"; ?></small></font></td>
What change do I need to make?
Thanks,
Tim
Tim Mousel
Fri 23rd Feb '01, 3:23am
Hi,
Just in case anyone ever needs to use this, here is the code I used successfully:
$tempdate = explode("-", $date);
$date = mktime(0,0,0,$tempdate[1],$tempdate[2],$tempdate[0]);
$newdate = date("m-d-y", $date);
Not sure why this works and the other didn't?
Tim
Tim Mousel
Sat 15th Feb '03, 5:42pm
I've come across this again. Is this code the best way to display a date from a mysql database using php version 4.2.3?
$tempdate = explode("-", $date);
$date = mktime(0,0,0,$tempdate[1],$tempdate[2],$tempdate[0]);
$newdate = date("m-d-y", $date);
Thanks,
Tim
Son-Goku
Sat 15th Feb '03, 7:00pm
instead of
$id = $row["id"];
$date = $row["date"];
$town = $row["town"];
$state = $row["state"];
$seller = $row["seller"];
$category = $row["category"];
$broker = $row["broker"];
$sellurl = $row["sellurl"];
$brokerurl = $row["brokerurl"];
you might want to use
extract($row);
the result is exactly the same, so you can still access sellurl via $sellurl... :)
I have a date field stored in a mysql table in this format: 2001-02-22
you don't really need all the time stamp formatting. if i got you right 2001-02-22 is what you allready have in your database, so this should do the job:
$date = explode("-", $date);
$date = $date[2]."-".$date[3]."-".$date[1];
Dark_Wizard
Tue 18th Feb '03, 11:52am
$date = explode("-", $date);
$date = $date[2]."-".$date[3]."-".$date[1];
Actually it should be:
$date = explode("-", $date);
$date = $date[1]."-".$date[2]."-".$date[0];
Tim Mousel
Sun 23rd Feb '03, 2:33am
Thanks for the help. I really like the
extract($row);
It will save a lot of time. Is there a way to use this with stripslashes?
As far as the date....what if I want to display the date in this format:
Feb 22, 2003
This code will do that:
[
$tempdate = explode("-", $date_added);
$date_added = mktime(0,0,0,$tempdate[1],$tempdate[2],$tempdate[0]);
$newdate = date("M d, Y", $date_added);
echo "$newdate";
but I'm not sure if using this method will give me flexibility in displaying different date formats:
$date = explode("-", $date);
$date = $date[1]."-".$date[2]."-".$date[0];
Is there a way to make that more flexible?
Thanks,
Tim
Icheb
Mon 24th Feb '03, 7:12am
The easiest way in my opinion to manipulate timestamps is by storing them as UNIX time.
You don't need a dozend of conversions before you have the result you want, just one call to a function and you have the desired result.
vBulletin® v3.8.0 Beta 3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.