468,256 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

More noob nonsense - output format for a timestamp date variable

hph
Okay, another trivial matter that I can't solve.

I have a variable - $lastdate - that is the latest date any record in a
MySQL database was updated. Its MySQL format is TIMESTAMP.

If I say [echo $lastdate] I get the output I'd expect - 20060424221549
which is a YYYYMMDDHHMMSS format.

I'd like to be able to display that using a format of mm/dd/yy with no
leading spaces on the month and day. I don't know what the php command
is to do this. In other words, I'm trying to display a variable -
$lastdate - formatted a certain way. I know that the formatting string
n/j/y will get my output looking as I'd like, but I can't figure out how
to apply that to my variable.

Thanks to those who've helped me with my questions the last two days.
This seems to be the last piece of information I can't find right now.
Apr 26 '06 #1
4 2236
date() requires a unix timestamp, which is differant from the mysql
timestamp. therefore, you must create one:
$yy = substr($lastdate, 0, 4);
$mm = substr($lastdate, 4, 2);
$dd = substr($lastdate, 6, 2);
$hh = substr($lastdate, 8, 2);
$mm = substr($lastdate, 10, 2);
$ss = substr($lastdate, 12, 2);
$date = mktime($hh, $mm, $ss, $mm, $dd, $yy);
$whatYouWant = date('n/j/y', $date);

hph wrote:
Okay, another trivial matter that I can't solve.

I have a variable - $lastdate - that is the latest date any record in a
MySQL database was updated. Its MySQL format is TIMESTAMP.

If I say [echo $lastdate] I get the output I'd expect - 20060424221549
which is a YYYYMMDDHHMMSS format.

I'd like to be able to display that using a format of mm/dd/yy with no
leading spaces on the month and day. I don't know what the php command
is to do this. In other words, I'm trying to display a variable -
$lastdate - formatted a certain way. I know that the formatting string
n/j/y will get my output looking as I'd like, but I can't figure out how
to apply that to my variable.

Thanks to those who've helped me with my questions the last two days.
This seems to be the last piece of information I can't find right now.


Apr 26 '06 #2
>I have a variable - $lastdate - that is the latest date any record in a
MySQL database was updated. Its MySQL format is TIMESTAMP.
I recommend using date_format() in MySQL.
If I say [echo $lastdate] I get the output I'd expect - 20060424221549
which is a YYYYMMDDHHMMSS format.

I'd like to be able to display that using a format of mm/dd/yy with no
In case you haven't heard, years have 4 digits. Ever hear about
the Y2K problem?
leading spaces on the month and day. I don't know what the php command
is to do this. In other words, I'm trying to display a variable -
$lastdate - formatted a certain way. I know that the formatting string
n/j/y will get my output looking as I'd like, but I can't figure out how
to apply that to my variable.


SELECT DATE_FORMAT(stamp, '%m/%d/%y'), ...
if you absolutely insist on writing non-Y2K-compliant code.

Gordon L. Burditt
Apr 26 '06 #3
hph
In article <12*************@corp.supernews.com>,
go***********@burditt.org (Gordon Burditt) wrote:

SELECT DATE_FORMAT(stamp, '%m/%d/%y'), ...
if you absolutely insist on writing non-Y2K-compliant code.

Gordon L. Burditt

Thanks.

$last_donation=mysql_query("SELECT DATE_FORMAT($year_data[2],
'%j/%n/%Y'");

gave me what I wanted. $last_donation was a variable that I later
echoed, and year_data was a result array into which I'd fetched data,
including a TIMESTAMP data field.

I'm all for Y2K compliance (I won't, however, be around when Y10K
becomes an issue...), but in this case, I'm simply displaying a date in
a format that, I think, seems to appeal to the majority of people in the
US who think of dates as just dates and not data.

Thanks for your patience in answering my trivial questions.
Apr 27 '06 #4
>>
SELECT DATE_FORMAT(stamp, '%m/%d/%y'), ...
if you absolutely insist on writing non-Y2K-compliant code.

Gordon L. Burditt

Thanks.

$last_donation=mysql_query("SELECT DATE_FORMAT($year_data[2],
'%j/%n/%Y'");

gave me what I wanted. $last_donation was a variable that I later
echoed, and year_data was a result array into which I'd fetched data,
including a TIMESTAMP data field.


This won't work. Best case, $last_donation is a MySQL result handle,
not a string. You need to fetch a row and get the right element
and echo *that*.

Ideally, you don't use a separate query, you get the timestamp in
the format you want it (using DATE_FORMAT) instead of the format MySQL
uses (just selecting the timestamp field).
I'm all for Y2K compliance (I won't, however, be around when Y10K
becomes an issue...), but in this case, I'm simply displaying a date in
a format that, I think, seems to appeal to the majority of people in the
US who think of dates as just dates and not data.

Thanks for your patience in answering my trivial questions.

Apr 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by point | last post: by
11 posts views Thread by Rick | last post: by
15 posts views Thread by Simon Brooke | last post: by
2 posts views Thread by dbztax | last post: by
9 posts views Thread by deko | last post: by
3 posts views Thread by Pravljica | last post: by
9 posts views Thread by Martin | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.