开发者

PHP: How to get the days of the week?

I'm wanting to store items in my database with a DATE value for a certain 开发者_Go百科day. I don't know how to get the current Monday, or Tuesday, etc. from the current week. Here is my current database setup.

menuentry
 id int(10) PK
 menu_item_id int(10) FK
 day_of_week date
 message varchar(255)

So I have a class setup that holds all the info then I was going to do something like this...

foreach ( $menuEntryArray as $item )
{
   if ( $item->getDate() == [DONT KNOW WHAT TO PUT HERE] )
   {
      // code to display menu_item information
   }
} 

So I'm just unsure what to put in "[DONT KNOW WHAT TO PUT HERE]" to compare to see if the date is specified for this week's Monday, or Tuesday, etc. The foreach above runs for each day of the week - so it'll look like this...

Monday
Item 1
Item 2
Item 3

Tuesday
Item 1

Wednesday
Item 1
Item 2

...

Thanks!


$day=date('l',strtotime($item['day_of_week']));


First thanks profitphp for your answer, but I was needing more. I don't think I was clear enough with my question perhaps. I finally got it working here is what I did...

function getCurrentWeek()
{
    $weekArray = array();

    // set current date1
    $date = date("m/d/Y"); //'04/30/2009';

    // parse about any English textual datetime description into a Unix timestamp
    $ts = strtotime( $date );

    // calculate the number of days since Monday
    $dow = date('w', $ts);
    $offset = $dow - 1;
    if ($offset < 0) $offset = 6;

    // calculate timestamp for the Monday
    $ts = $ts - $offset*86400;

    // loop from Monday till Sunday
    for ($i=0; $i<7; $i++, $ts+=86400){
        $temp_date = date("Y-m-d", $ts);  // here I set it to the same format as my database
        array_push( $weekArray, $temp_date );
    }

    return $weekArray;

}

Then I used that with this...

    foreach ( $menu as $item )
    {
        if ( $item->getDayOfWeek() == $weekArray[0] )
        {
            // do my stuff
        }
    }

Hope this helps someone, thanks again profitphp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜