0

So I'm making a website for this pizzeria.

In the past I made a simple window that pops up Monday-Friday & Between 11.30am-02:00pm which is simple enough.

But now I've had a request to do the same thing, but instead of having a single "Lunch Buffe Ongoing!" box, the client want's a daily menu (different menu every day, which is also easy enough), but he has 3 different daily menus alternating every week.

So here's an example: Week 1 Menu: Monday: w1m1 Tuesday: w1m2 ..

Week 2 Menu: Monday: w2m1 Tuesday: w2m2

So my question is: Is there a PHP function / equation for calculating "is it the first, second or third" week from a specified starting date

Edit (Not duplicate because): This isn't a duplicate of PHP get number of week for month because I'm not asking for the week number, I'm asking what week this is alternating with 3

Answer:

<?php
    function menuNumber($now) {
        $then = '26/05/2014';
        $first = DateTime::createFromFormat('d/m/Y', $then);
        $second = DateTime::createFromFormat('d/m/Y', $now);
        $calculated = (floor($first->diff($second)->days/7) % 3) + 1;
        return $calculated;
    }
    echo "Menu Number " . menuNumber(date("d/m/Y")); // Menu Number 1
?>

Help From: php weeks between 2 dates

Community
  • 1
  • 1
Richard Muthwill
  • 320
  • 2
  • 16
  • There are SQL functions to do that. Do you have a database connection? – wallyk Jun 17 '14 at 17:43
  • To the person that posted the "same question", no it's not.. I'm asking "is this week 1, 2 or 3" since the starting week (which I'll specify) Yes @wallyk , I do :) – Richard Muthwill Jun 17 '14 at 17:47
  • @JohnConde Can you please unmark this as a duplicate so I can post my answer to anyone that wishes to see this in future, in the "Edit" section I explained why it wasn't a duplicate – Richard Muthwill Jun 17 '14 at 21:37
  • @wallyk Would you also suggest using MS-Excel if there exists such a macro? – Markus Malkusch Jun 18 '14 at 16:58
  • @MarkusMalkusch: probably not, unless there was reason to suspect his PHP installation was already linked to Excel. I think I have a few PHP programs which do not connect to MySQL, but all the rest do. – wallyk Jun 18 '14 at 17:00
  • @RichardMuthwill "week of year" and modulo are your friends. Both available in PHP only. – Markus Malkusch Jun 18 '14 at 17:01

1 Answers1

0

PHP

<?php
    function menuNumber($now) {
        $then = '26/05/2014';
        $first = DateTime::createFromFormat('d/m/Y', $then);
        $second = DateTime::createFromFormat('d/m/Y', $now);
        $calculated = (floor($first->diff($second)->days/7) % 3) + 1;
        return $calculated;
    }
    echo "Menu Number " . menuNumber(date("d/m/Y")); // Menu Number 1
?>

Help From: PHP weeks between 2 dates

Community
  • 1
  • 1
Richard Muthwill
  • 320
  • 2
  • 16