By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

More noob nonsense - output format for a timestamp date variable

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
>>
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.