NYCPHP Meetup

NYPHP.org

[nycphp-talk] Week # and Corresponding Start and End Dates

Mark Armendariz nyphp at enobrev.com
Fri Apr 16 14:33:54 EDT 2004


Well, you could add a week_of_year column and a day_of_week column to your
sql:
 
SELECT
    *, 
    DATE_FORMAT(date_column, '%y') as year,        // for the 2 digit year
    DATE_FORMAT(date_column, '%b') as month,    // for the 3 letter month
    DATE_FORMAT(date_column, '%e') as day
    DAYOFWEEK(date_column) as day_of_week,
    WEEK(date_column) as week_of_year,
    DATE_FORMAT(DATE_ADD(date_column, INTERVAL 6 DAY), '%e') as last_day
FROM my_table
ORDER BY date_column DESC
 
And then in your code, if the day of week is 0, you show the week number and
add 7 for the date of the last day of the week:
 
 
<?php // BEGIN LOOP ?>
    <?php if ($data['day_of_week'] == 0) { ?>
        Week <?= $data['week_of_year'] ?>
        (<?= $data['month'] ?> <?= $data['day'] ?> - <?= $data['last_day']
?>, <?= $data['year'] ?><br />
        ------------------------<br />
    <?php } ?>
    <?= $data['month'] ?> <?= $data['day'] ?>, <?= $data['year'] ?>
<?php // END LOOP ?>
 
Haven't tested it, but that's how I would probably go about it (maybe
combine a couple date formats for a long and short format or something, but
i figured split them in case the end of the week date happens to be in the
next month, then you would want to add another column for that month and
show it when necessary.
 
 
Good Luck
 
Mark



  _____  

From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of invisiblemute
Sent: Friday, April 16, 2004 1:59 PM
To: talk at lists.nyphp.org
Subject: [nycphp-talk] Week # and Corresponding Start and End Dates


I have a list of date entries in a DB and I am able to pull them out nicely
by week with MySQL. I can't seem to find if there is then a good solution
with MySQL, PHP or some combination of the two for me to display the results
like this:
Week 1 (Jan 1 - 7, 04)
----------
Jan 1, 04
Jan 3, 04

Week 2 (Jan 8 - 15, 04)
----------
Jan 8, 04
Jan 9, 04
Jan 11, 04

I'm just missing the part in parentheses so I can display start and end
times for the weeks.

Thanks.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20040416/46d5114f/attachment.html>


More information about the talk mailing list