Dec 27

php trash day script

Tag: phperm @ 10:25 pm

Trash days on Monday suck.
I constantly forget to take out the trash on Saturday when there is a holiday on Monday. I dusted off the old trash-day-php script and installed it.

Installation is pretty simple.
Edit the script with the values you want.
place it in /etc/cron.daily/trash-day-php
chmod +x /etc/cron.daily/trash-day-php

cron doesn’t like . in filenames. Thats why I named the file trash-day-php not trash-day.php.

To test your email just run /etc/cron.daily/trash-day-php –test

#!/usr/bin/php -q
<?php
    require_once "/usr/share/php/libphp-phpmailer/class.phpmailer.php";
    // sudo apt-get install libphp-phpmailer # to install phpmailer

    $trash_day = 1; // uses date('w'); 0 = Sunday 6 = Saturday
    $from = "email@domail.com";
    $from_name = 'First M. Last *trash day* script.';

    $emails = array(
        "email-recipient@domain.com" => "First M. Last",
        "email-recipient2@domain.com" => "First M. Last",
    );
    // http://www.emailtextmessages.com/ // you can send yourself an sms

    $test_emails = array(); // You can setup test email addresses
                            // to be used with trash-day-php --test
                            // if you don't set this $emails will be used.
    
    /*
      Federal Legal Holidays 2009
    • January 1, 2009: New Year's Day [Jan. 1 every year]
    • January 19, 2009: Martin Luther King Day [3rd monday in Jan]
    • January 20, 2009: Inauguration Day [every 4th year]
    • February 16, 2009: Presidents Day (observed) [3rd monday in Feb]
       note: Presidents Day is also Washington's Birthday (observed)
    • May 25, 2009: Memorial Day (observed) [last monday in May]
    • July 4, 2009: Independence Day [July 4 every year]
    • September 7, 2009: Labor Day [1st monday in Sept]
    • October 12, 2009: Columbus Day (observed) [2nd monday in Oct]
    • November 11, 2009: Veterans' Day [Nov. 11 every year]
    • November 26, 2009: Thanksgiving Day [4th thursday in Nov]
    • December 25, 2009: Christmas Day [Dec. 25 every year]


    • January 1, 2008: New Year's Day [Jan. 1 every year]
    • January 21, 2008: Martin Luther King Day [3rd monday in Jan]
    • February 18, 2008: Presidents Day (observed) [3rd monday in Feb]
       note: Presidents Day is also Washington's Birthday (observed)
    • May 26, 2008: Memorial Day (observed) [last monday in May]
    • July 4, 2008: Independence Day [July 4 every year]
    • September 1, 2008: Labor Day [1st monday in Sept]
    • October 13, 2008: Columbus Day (observed) [2nd monday in Oct]
    • November 11, 2008: Veterans' Day [Nov. 11 every year]
    • November 27, 2008: Thanksgiving Day [4th thursday in Nov]
    • December 25, 2008: Christmas Day [Dec. 25 every year]

    Holiday     Definition
    ML King     3rd Monday of Jan     Mon, Jan 21st
    Presidents Day     3rd Monday of Feb     Mon, Feb 18th
    Memorial Day     Last Monday of May     Mon, May 26th1
    Labor Day     1st Monday of Sep     Mon, Sep 1st
    Columbus Day     2nd Monday of Oct     Mon, Oct 13th
    Election Day     Tuesday after 1st
    Monday of Nov     Tue, Nov 4th2
    Thanksgiving Day     Last Thursday of Nov     Thu, Nov 27th

    */

    $min = 60;
    $hr = $min * 60;
    $day = 24 * $hr;
    $week = $day * 7;

    function calculate_day($data) {
        $first_day_of_month = strtotime($data['month'] . ' 1');
        $last_day_of_month = strtotime($data['month'] . ' ' . date('t', $first_day_of_month));
        $min = 60;
        $hr = $min * 60;
        $day = 24 * $hr;
        $week = $day * 7;
        // 'w'
        // print_r($data);
        if (!is_numeric($data['day'])) {
            $data['day'] = date('w',strtotime($data['day']));
        }

        if ($data['offset'] > 0) {
            $data['offset']--;
        }
        if ($data['offset'] >= 0) {
            // count up
            $target = $first_day_of_month;
            // Keep adding a week until offset is 0.
            $target = ($target + ($week * $data['offset']));
            
            // Keep addinga day until the day of the week matches.
            while (date('w', $target) != $data['day']) {
                $target = ($target + $day);
                # echo "day:", date('w',$target),"\n";
            }
            # echo "Figured:",date('r', $target),"\n";
            return $target;
        } else {
            // count down.
            // Last Monday of May     Mon, May 26th
            $target = $last_day_of_month;
            // Keep subtracting a week until offset is 0.
            $target = ($target - ($week * abs($data['offset']+1)));
            $multiple = date('w',$target) - $data['day'];
            // Keep taking away a day until the day of the week matches.
            while (date('w', $target) != $data['day']) {
                $target = ($target - $day);
                # echo "day:", date('w',$target),"\n";
            }
            # echo "-Figured:",date('r', $target),"\n";
            return $target;
        }
    }

    function send_emails($from, $from_name, $emails, $holiday) {
        $mail = new PHPMailer();
        # $mail->AddAddress("erm@the-erm.com,steph@the-erm.com,theerm@gmail.com");
        // 10digitphonenumber@tmomail.net
        foreach ($emails as $email=>$name) {
            $mail->AddBCC($email,$name);
        }
        $mail->From     = $from;
        $mail->FromName = $from_name;
        $mail->Subject = "*** TRASH DAY ".$holiday." ***";
        $mail->Body = "$holiday will occur on a Monday, remember to set out the trash on Saturday.\n";
        if(!$mail->Send()) {
            echo "There has been a mail error sending to:\n";
            print_r($emails);
            exit;
        } else {
            echo "Sent email to:\n";
            print_r($emails);
        }
    }

    /*
    0 (for Sunday) through 6 (for Saturday)
    */
    $holidays = array(
        'New Years' => strtotime('January 1 '.(date("Y")+1)),
        'Martin Luther King Day' => calculate_day(
            array(
                'day' => 'Monday',
                'offset' => 3,
                'month' => 'January'
            )
        ),
        'Presidents Day' => calculate_day(
            array(
                'day' => 'Monday',
                'offset' => 3,
                'month' => 'February'
            )
        ),
        'Memorial Day' => calculate_day(
            array(
                'day' => 'Monday',
                'offset' => -1,
                'month' => 'May'
            )
        ),
        'Independence Day' => strtotime('July 4th'),
        'Labor Day' => calculate_day(
            array(
                'month' => 'September',
                'day' => 'Monday',
                'offset' => 1
            )
        ),
        'Columbus Day' => calculate_day(
            array(
                'month' => 'October',
                'day' => 'Monday',
                'offset' => 2
            )
        ),
        'Veterans\' Day' => strtotime('November 11'),
        'Thanksgiving' => calculate_day(
            array(
                'month' => 'November',
                'day' => 'Thursday',
                'offset' => -1
            )
        ),
        'Christmas' => strtotime('December 25'),
        'Christmas Eve' => strtotime('December 24')
    );

    $today = time();

    foreach ($holidays as $holiday=>$v) {
        if ($v >= $today && $v <= ($today + $week))  {
            echo "In range $holiday\n";
            if (date('w',$v) == $trash_day) {
                echo "Date is on a Monday\n";
                echo "***** EMAIL ***** ";
                send_emails($from, $from_name, $emails, $holiday);
            } else {
                echo "Date is not a Monday\n";
            }
        }
        echo "$holiday ",date('r',$v),"\n";
    }

    if (in_array ("--test", $argv)) {
        if (!$test_emails) {
            $test_emails = $emails;
        }
        send_emails($from, $from_name, $test_emails, "TEST EMAIL");
    }

Leave a Reply

You must be logged in to post a comment.