Automatically displaying php notice on certain dates

Discussion in 'PHP' started by realitybend, Feb 24, 2010.

  1. #1
    I need to use php (or html or javascript, I guess, if that would work better) to create a message that displays the week before christmas, fourth of july, etc. How would I go about doing that?

    Thanks for your help.
     
    realitybend, Feb 24, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    here's one way to check if its one week before :

    
    if (date('d M') == '18 Dec')
    {
    echo "week before xmas";
    }
    
    Code (markup):

    but that's just checking a single date, if you wanted to say countdown the days till xmas. this would work but I'm sure there's a better solution. As its the 24th, I switched Feb for Dec to test it.

    
    
    if(date('M') == 'Dec')
    {
    if((date('d') >= 18) && (date('d') <= 25))
    {
    $days =  25 - date('d')  ;
    echo "$days days till xmas";
    }
    if(date('d') == '25') { echo "Merry Xmas"; }
    }
    
    Code (markup):
     
    shallowink, Feb 24, 2010 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Try this script. $dates is an array that stores the dates you want the messages for (m-d). $me are messages for corresponding dates.

    
    $dates=array('02-27','12-25','07-04'); $t=time();
    $me=array('test message', 'message for christmas', 'message for july 4');
    $message=''; $count=0;
    foreach($dates as $v){
    $v=explode('-',$v); $m=$v[0]; $d=$v[1];
    $tt= mktime(0,0,0,$m,$d,date("Y")); 
    if($t<$tt){
    if( ($tt-$t)<=(7*24*3600) ){ $message=$me[$count]; }
    }
    }
    echo $message;
    
    Code (markup):

    Thanks :)
     
    JEET, Feb 24, 2010 IP
  4. realitybend

    realitybend Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks. I used the code below, but found I needed it to do more than I mentioned before. How would I calculate the date of a holiday like memorial day, so the week before the holiday it says "Memorial day is on May 25"? The actual date changes each year. I'd need to do it for Memorial Day, Labor Day, and Thanksgiving.

    Thanks for helping out someone who really doesn't know what they're doing but is trying to learn.

    $dates=array('02-27','12-25','07-04'); $t=time();
    $me=array('test message', 'message for christmas', 'message for july 4');
    $message=''; $count=0;
    foreach($dates as $v){
    $v=explode('-',$v); $m=$v[0]; $d=$v[1];
    $tt= mktime(0,0,0,$m,$d,date("Y")); 
    if($t<$tt){
    if( ($tt-$t)<=(7*24*3600) ){ $message=$me[$count]; }
    }
    }
    echo $message;
    PHP:
     
    realitybend, Mar 25, 2010 IP
  5. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    try this
    
    <?php
    $data = array("name" => "Holiday",
                        "date" => "25-12-2010",
                        "message" => "Yeah, it's holiday");
    
    $current = strtotime(date("d-m-Y",time());
    $target = strtotime($data['date']);
    
    $day = round(abs($target - $current) / (60*60*24),0);
    // 1 day before holiday
    if($day == 1){
         echo $data['message'];
    }
    ?>
    
    PHP:
     
    guardian999, Mar 25, 2010 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    did they move memorial day?
     
    shallowink, Mar 25, 2010 IP
  7. realitybend

    realitybend Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, they didn't move memorial day. :) However, it's last Monday of May, so the actual numerical date changes.

    @guardian999 - I need to actually say the date, like 'Memorial day is on May 25', so I can't just have a generic message saying 'Yeah it's a holiday'. Thanks, though.
     
    realitybend, Mar 25, 2010 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    it was more of a comment on him using 12-25 as the date, just duplicated the code that was already posted. finding holidays with a moving date is a bit harder. and the process would depend on the holiday. but hey google is your friend and mine too. see if this helps you out :

    http://michaelthompson.org/technikos/holidays.php
     
    shallowink, Mar 25, 2010 IP
  9. realitybend

    realitybend Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, I understand each individually, I just have no idea how to put it together without messing the other up.
     
    realitybend, Mar 25, 2010 IP