Skip to main content
Participant
April 8, 2008
Question

php format date

  • April 8, 2008
  • 2 replies
  • 593 views
I'm retrieving a date record from my mysql table row. However, I'd like to reuse this data and show by sometimes showing just the month and day of the week (apr wed); and at other times just the month and date (apr 23).

Here's the code I'm using, which obviously isn't working.
<?php echo date($row_rsGigs['date_gig'], 'M D'); ?>

Any help would be appreciated.
This topic has been closed for replies.

2 replies

Inspiring
April 8, 2008
trs.net wrote:
> Here's the code I'm using, which obviously isn't working.
> <?php echo date($row_rsGigs['date_gig'], 'M D'); ?>

MySQL dates are stored in human readable format, such as 2008-04-08. The
PHP date() function requires a Unix timestamp. The two don't mix.

Even if they did mix, you have the arguments the wrong way round in the
date() function. The formatting characters come first, not the date.

The best way to deal with your requirement is to get the SQL query to
handle everything for you.

SELECT DATE_FORMAT(date_gig, '%b') AS mon,
DATE_FORMAT(date_gig, '%a') AS weekday,
DAYOFMONTH(date_gig) AS day
FROM myTable

That gives you $row_rsGigs['mon'] as the abbreviated month,
$row_rsGigs['weekday'] as the abbreviated day of the week, and
$row_rsGigs['day'] as the day of the month.

Learn all about MySQL date and time functions here:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
April 8, 2008
Is this what you are trying to do?

<?php echo date('M D', strtotime($row_rsGigs['date_gig'])); ?>

--
Ken Ford
Adobe Community Expert - Dreamweaver
Fordwebs, LLC
http://www.fordwebs.com


"trs.net" <webforumsuser@macromedia.com> wrote in message
news:ftek4p$cg3$1@forums.macromedia.com...
> I'm retrieving a date record from my mysql table row. However, I'd like
> to
> reuse this data and show by sometimes showing just the month and day of
> the
> week (apr wed); and at other times just the month and date (apr 23).
>
> Here's the code I'm using, which obviously isn't working.
> <?php echo date($row_rsGigs['date_gig'], 'M D'); ?>
>
> Any help would be appreciated.
>