Home > Archive > PHP Programming > April 2006 > More noob nonsense - output format for a timestamp date variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
More noob nonsense - output format for a timestamp date variable
|
|
|
| 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.
| |
| Joshie Surber 2006-04-25, 9:58 pm |
| 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.
| |
| Gordon Burditt 2006-04-26, 3:58 am |
| >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
| |
|
| In article <124u2qjs1abpr56@corp.supernews.com>,
gordonb.787qs@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.
| |
| Gordon Burditt 2006-04-26, 9:58 pm |
| >>
>
>
>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.
|
|
|
|
|