Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Date from query output 1

Status
Not open for further replies.

skicamel

Programmer
Dec 24, 2001
126
US
This seems like such a basic thing. I'm almost embarassed to ask, but I haven't found anything that I could get to work. I've got a page that populates a table with query results from another page. The first column is the date column. I continually get the full 2002-03-28 00:00:00.000 format. I found a way to convert a timestamp... Do I need to convert to a timestamp and then use the date function to pull it back? How can I manipulate the following to change each row of the query results?


while (odbc_fetch_into($result,$myrow))
{
$date = $myrow[0];
}
echo &quot;<TR><td>$date</td> etc...&quot;;

Of course, there's a fair amount of text before, during, and after these lines, but I tried to post the minimum relevant portion.
 
date_format($date, '%Y-%m-%d')

will return 2002-03-28

you can move these around to suit yourself:
date_format($date, '%d/%m/%y')
will return 28/03/02 ***************************************
Party on, dudes!
 
Ummmm.... date_format()? I can't find any documentation on that one karver... do you have an links to info on this function? It's not on PHP.net!

They do have something similar though:
Code:
$your_date = &quot;2002-03-27&quot;;

$your_date = date(&quot;m.d.Y&quot;,$your_date);
echo &quot;$your_date&quot;; // prints 27.03.2002

$your_date = date(&quot;d/m/Y&quot;,$your_date);
echo &quot;$your_date&quot;; // prints 03/27/2002

Check out for more info on all the possible formatting options.

Good luck!

BTW, it's not a great idea to call a variable $date because PHP's date() function has the same name and could conflict! -gerrygerry
Go To
 
What your showing was the same thing I'd found...Try running this...

echo date(&quot;m.d.Y&quot;,'03-02-2002');

You get back 12.31.1969

Tried this with a variety of dates and always get back 12.31.1969. Any ideas?

(thanks for the variable naming tip...using @date in sql for so long, didn't think about changing it while learning php.)
 
Oh... whoops i should have remembered! I was thinking about something I was just working on and I wasn't sure what solution I had come up with. Try this instead (this is what I used):
Code:
$mydate &quot; &quot;2002-03-27&quot;;
ereg(&quot;([0-9]{4})-([0-9]{2})-([0-9]{2})&quot;, $mydate, $regs); 

echo &quot;$regs[1]&quot;; // prints 2002
echo &quot;$regs[2]&quot;; // prints 03
echo &quot;$regs[3]&quot;; // prints 27

Now, just display these in whatever order you want.

Good luck! -gerrygerry
Go To
 
Getting closer. Works great the way you have it there, but I can't seem to get it to display in the table...
Taking what I had initially:

while (odbc_fetch_into($result,$myrow))
{
$mydate = $myrow[0];
}
echo &quot;<TR><td>$mydate</td> etc...&quot;;

I tried a few variations of what you had:

ereg(&quot;([0-9]{4})-([0-9]{2})-([0-9]{2})&quot;, $mydate, $regs);

$d1 = &quot;$regs[1]&quot;;
$d2 = &quot;$regs[2]&quot;;
$d3 = &quot;$regs[3]&quot;;

echo &quot;<TR><td>$d1/$d2/$d3</td> etc...&quot;;
or
echo &quot;<tr><td>&quot;.$d1.&quot;/&quot;.$d2.&quot;/&quot;.$d3.&quot;</td> etc...&quot;;
--------------------------------------------
ereg(&quot;([0-9]{4})-([0-9]{2})-([0-9]{2})&quot;, $mydate, $regs);

echo &quot;<TR><td>$regs[1]/$regs[2]/$regs[3]</td> etc...&quot;;
or
echo &quot;<tr><td>&quot;.$regs[1].&quot;/&quot;.$regs[2].&quot;/&quot;.$regs[3].&quot;</td> etc...&quot;;
----------------------------------------------

Still not getting anything. Either get nothing returned or I get &quot; // &quot; returned. Any idea how to get these to show in the table? Thanks again for the help. I'll dig into substring and see what I can get there.
 
Okay. We've got 'er. Substring worked like a charm. Thanks for the input.

$dy = substr($date, 0, 4);
$dm = substr($date,5,2);
$dd = substr($date,8,2);

echo (&quot;<TR><td>$dm/$dd/$dy</td> ...etc&quot;);
 
You mean the following didn't work?
Code:
while (odbc_fetch_into($result,$myrow)) 
{
 $mydate = $myrow[0];
 ereg(&quot;([0-9]{4})-([0-9]{2})-([0-9]{2})&quot;, $mydate, $regs); 
 echo &quot;<TR><TD>&quot;.$regs[2].&quot;/&quot;.$regs[3].&quot;/&quot;.$regs[3].&quot;</TD></TR>&quot;;
}

That's strange... I have this exact thing working on my server (only with mysql instead of odbc)! -gerrygerry
Go To
 
You mean the following didn't work?
Code:
while (odbc_fetch_into($result,$myrow)) 
{
 $mydate = $myrow[0];
 ereg(&quot;([0-9]{4})-([0-9]{2})-([0-9]{2})&quot;, $mydate, $regs); 
 echo &quot;<TR><TD>&quot;.$regs[2].&quot;/&quot;.$regs[3].&quot;/&quot;.$regs[3].&quot;</TD></TR>&quot;;
}

That's strange... I have this exact thing working on my server (only with mysql instead of odbc)! -gerrygerry
Go To
 
That's odd. I honestly don't know what I was doing differently at work...no, that works great. Now I'm more stumped by what I had did differently at work... hmm. I'll stick w/ substring for now since I'm already familiar with it in MSSQL, but definitely, thanks, that absolutely works great. (This will drive me nuts, though...I know I copied and pasted earlier today)... Anyway, thanks again. Finally nailed it. Good show, gerrygerry. :)

(It's always good to be humbled once in awhile, eh?)
 
Woops, been away for the weekend, I should have pointed out that date_format() is used in the sql query , not in php - basically you get your formatted date directly from sql.

$query = &quot;select field1,field2,date_format(field3, '%Y-%m-%d'),field4 from table where stuff&quot;;

and so no other formattting is necessary. ***************************************
Party on, dudes!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top