How Much Time Is Left?

I wanted to write a quick script that would send an email reminding me of how much time was left until a particular event. This is what I came up with!

<?php
/**
 * Sends an email with a 'time left' reminder
 */

/**
 * Event Name
 * This will be used as the subject line
 */
$event = 'YOUR EVENT NAME';

/**
 * Date
 * This is used to calculate how much time is left.
 * Sample dates:
 * Month Day, Year
 * Month Day, Year at hour:minute am/pm
 */
$date = 'February 10, 2013 at 3pm';

// Email (or possibly txt!)
/**
 * Email (or possibly SMS aka text message)
 * Carrier domains
 * XXXXXXXXXX@txt.att.net (AT&T)
 * XXXXXXXXXX@vtext.com (Verizon)
 * XXXXXXXXXX@vmobl.com (Virgin Mobile)
 * XXXXXXXXXX@messaging.sprintpcs.com (Sprint)
 * See more at http://www.venture-ware.com/kevin/web-development/email-to-sms/
 */
$email = 'user@domain.com';

/**
 * You shouldn't have to modify anything
 * below here....but you can if you want!
 */

mail($email, $event, time_left((strtotime($date) - time())));

/**
 * Returns a string of time left
 * The biggest unit is weeks
 * @author CoderZone
 * @url http://coderzone.org/library/Time-left-In-Weeks-Days-Hours-And-Seconds_979.htm
 */
function time_left($integer){
 
$seconds=$integer;
 
  if (
$seconds/60 >=1){
   
$minutes=floor($seconds/60);
   
    if (
$minutes/60 >= 1){ // Hours
     
$hours=floor($minutes/60);
     
      if (
$hours/24 >= 1){ //days
       
$days=floor($hours/24);
       
        if (
$days/7 >=1){ //weeks
         
$weeks=floor($days/7);
         
          if (
$weeks>=2) $return="$weeks Weeks";
          else
$return="$weeks Week";
       
        }
//end of weeks

        $days=$days-(floor($days/7))*7;

        if ($weeks>=1 && $days >=1) $return="$return, ";
        if (
$days >=2) $return="$return $days days";
        if (
$days ==1) $return="$return $days day";

      } //end of days

      $hours=$hours-(floor($hours/24))*24;

      if ($days>=1 && $hours >=1) $return="$return, ";
      if (
$hours >=2) $return="$return $hours hours";
      if (
$hours ==1) $return="$return $hours hour";

    } //end of Hours

    $minutes=$minutes-(floor($minutes/60))*60;

    if ($hours>=1 && $minutes >=1) $return="$return, ";
    if (
$minutes >=2) $return="$return $minutes minutes";
    if (
$minutes ==1) $return="$return $minutes minute";

  } //end of minutes

  $seconds=$integer-(floor($integer/60))*60;

  if ($minutes>=1 && $seconds >=1) $return="$return, ";
  if (
$seconds >=2) $return="$return $seconds seconds";
  if (
$seconds ==1) $return="$return $seconds second";
  
  return
$return;
}
?>

Categories: 

1 comment

by Andre (not verified) on Sun, 11/25/2012 - 19:48

wow! this is great! Thanks!

How Much Time Is Left? | Gerrit Brands

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /homepages/29/d196880538/htdocs/drupal7/includes/common.inc:2681) in drupal_send_headers() (line 1212 of /homepages/29/d196880538/htdocs/drupal7/includes/bootstrap.inc).
  • PDOException: SQLSTATE[42000]: Syntax error or access violation: 1142 INSERT command denied to user 'dbo354826752'@'74.208.16.6' for table 'watchdog': INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => cron [:db_insert_placeholder_2] => Attempting to re-run cron while it is already running. [:db_insert_placeholder_3] => a:0:{} [:db_insert_placeholder_4] => 4 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://gerritbrands.com/blog/how-much-time-left [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => 54.225.18.67 [:db_insert_placeholder_9] => 1500647607 ) in dblog_watchdog() (line 154 of /homepages/29/d196880538/htdocs/drupal7/modules/dblog/dblog.module).

Error

The website encountered an unexpected error. Please try again later.