Jump to content

Pull mysql value based on date


sweetback

Recommended Posts

Hello,

 

I am new to php so I am sorry if this is an easy question. I am looking to make a little box on my site that will update itself based on the date. I run a group that meets on Sunday evening, and would like for this event box to update itself and show the event that will be coming up on Sunday. So, hypothetically, each Monday, it would update and show the next event (which would take place Sunday). Is there a way to do this with mysql and php?

 

Thanks so much for any help you can give!

Link to comment
https://forums.phpfreaks.com/topic/124020-pull-mysql-value-based-on-date/
Share on other sites

Here's how I'd probably do it.  (Tested and working)

<?php
for($i=0; $i<=6; $i++){
    if(date('l', mktime(0, 0, 0, date('m'), date('d')+$i, date('Y')))=='Sunday'){
        $next_sunday=date('m-d-Y', mktime(0, 0, 0, date('m'), date('d')+$i, date('Y')));
    }
}
echo 'Next Sunday\'s date is '.$next_sunday;
?>

Thanks Chris! That works really well! I really appreciate your help with this.

 

So how would I then apply this to get the code to pull a value (in this case a text string of the upcoming event) that was associated with the upcoming Sunday.

 

for instance... I would like to have box that says something like: THIS SUNDAY, and then below that, a string that would automatically update and show what the next event is.

Your best bet for this is to do everything purely in MySQL

 

Set up your table with a `date` column of DATE type

 

Then simply use a query like this

 

SELECT `description`

  FROM `events`

  WHERE `date` BETWEEN

    (CURRENT_DATE - INTERVAL DAYOFWEEK(NOW())-2 DAY)

    AND

    (CURRENT_DATE + INTERVAL 8-DAYOFWEEK(NOW()) DAY);

 

Which will columns where `date` is between the Monday and the Sunday of the current week.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.