Jump to content

Iambr

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Iambr's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hello fellas I'm a very newbie with codding, but i wanted to get a date time calculator to help me calculate dates without a margin of error, easily and fast. So i went ahead and googled it, found one very good, but way too complicated for me to understand, still i gave it a try and spent a nice amount of hours trying to figure out the code and removing / hiding the unnecessary parts for the result i want. Anyway, the code is here, 2 php files; http://rapidshare.com/files/406520770/datetimecalctest.rar.html or code below; <?php require_once "class.datetimecalc.php"; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Date and Time Calculator - Examples, Usage, and Syntax</title> <style type="text/css"> <!-- body,td,th { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; } .style1 { color: #FFFFFF; font-weight: bold; } .biggertextbox { width: 250px; } .red {color: #ff0000;} .blue {color: #0000ff;} .code {color: #179D17;} .green {color: #003300;} --> </style></head> <body> <br> <?php if ($_POST['Submit'] == "Calculate") { $obj = new Date_Time_Calc($_POST['input'], $_POST['mask']); if ($_POST['calc'] == "Add") { $output = $obj->add($_POST['unit_of_time'], $_POST['howmuch'], true); $syntax = '$obj = new Date_Time_Calc("' . $_POST['input'] . '", "' . $_POST['mask'] . '");<br>'; $syntax .= 'echo $obj->add("' . $_POST['unit_of_time'] . '", ' . $_POST['howmuch'] . ');'; } else { $output = $obj->subtract($_POST['unit_of_time'], $_POST['howmuch'], true); $syntax = '$obj = new Date_Time_Calc("' . $_POST['input'] . '", "' . $_POST['mask'] . '");<br>'; $syntax .= 'echo $obj->subtract("' . $_POST['unit_of_time'] . '", ' . $_POST['howmuch'] . ');'; } } else { $_POST['input'] = "12 Jul 2010 12:00"; $_POST['mask'] = "d M Y H:i"; $_POST['calc'] = "Add"; $_POST['spawn'] = "Value1"; $_POST['unit_of_time'] = "hour"; $_POST['howmuch'] = 16; } ?> <form action="test.php" method="post"> <table width="100%" border="0"> <tr> <td colspan="2" align="center" bgcolor="#003300"><span class="style1">Lineage 2 Raid Boss Calculator</span></td> </tr> <tr> <td width="45%" align="right"> </td> <td width="55%"> </td> </tr> <?php if ($_POST['Submit'] == "Calculate") { ?> <tr> <td align="right" class="red"><b>Window Starts:</b></td> <td width="55%" class="red"><?php echo $output;?></td> </tr> <?php } ?> <tbody id="Row1" style="display:none"> <tr> <td align="right">Add2:</td> <td><select name="calc" id="calc" class="biggertextbox"> <?php if ($_POST['calc'] != "") { ?> <option value="<?php echo $_POST['calc'];?>"><?php echo $_POST['calc'];?></option> <?php } ?> <option value="Add">Varka</option> <option value="Subtract">Ketra</option> </select> </td> </tr> </tbody> <tr> <td align="right">Time of Death: </td> <td><input name="input" type="text" id="input" class="biggertextbox" value="<?php echo $_POST['input'];?>"></td> </tr> <tbody id="Row1" style="display:none"> <tr> <td align="right">Mask:</td> <td><input name="mask" type="text" id="mask" class="biggertextbox" value="<?php echo $_POST['mask'];?>"></td> </tr> <tr> <td align="right">Hours: </td> <td><select name="unit_of_time" id="unit_of_time" class="biggertextbox"> <?php if ($_POST['unit_of_time'] != "") { switch ($_POST['unit_of_time']) { case "hour": $formal_unit = "Hours"; break; } ?> <option value="<?php echo $_POST['unit_of_time'];?>"><?php echo $formal_unit;?></option> <?php } ?> <option value="hour">Hours</option> </select> </td> </tr> </tbody> <tr> <td align="right">Respawn Time: </td> <td><input name="howmuch" type="text" id="howmuch" class="biggertextbox" value="<?php echo $_POST['howmuch'];?>"></td> </tr> <tr> <td align="right">Spawn Option:</td> <td><select name="spawn" id="spawn" class="biggertextbox"> <?php if ($_POST['spawn'] != "") { ?> <option value="<?php echo $_POST['spawn'];?>"><?php echo $_POST['spawn'];?></option> <?php } ?> <option value="Add">Value2</option> <option value="Add">Value3</option> </select> </td> </tr> <tr> <td align="right"> </td> <td><input type="submit" name="Submit" value="Calculate"></td> </tr> <tr> <td align="right"> </td> <td> </td> </tr> </table> <center> <table border="0"> <tr> <td> <b><span class="green">Syntax: </span></b> <br> <span class="code"> <?php if ($_POST['Submit'] == "Calculate") { echo $syntax; } ?> </span> </td> </tr> </table> </center> </form> </body> </html> <?php /** * Date and Time Calculator by Elac v0.9.3 * [email protected] * www.elacdude.com * * You are free to use this code free of charge, modify it, and distrubute it, * just leave this comment block at the top of this file. * * * Changes/Modifications * 6/24/08 - Version 0.9.2 released. Minor additions * - Added "S" support. (th, rd, st, nd. example: 5th) * - Added a few more abbreviations for units of time in calculate() (s. sec. secs. min. mins. m. and more) * - Added example.php (php examples and usage) and date_time_formats.html (list of supported date/time formats) to the package. * 6/25/08 - Version 0.9.3 released. Bug fixes * - Fixed month subtraction (wrap to previous year) bug * - Fixed month and year "$only_return_the_value=true" bug. If you calculated by months or years, and set * $only_return_the_value=true, it would overwrite the values instead of just returning them. * - Fixed the "D" (Sun, Mon, Tue) bug. If you supplied "D" and "d" in the same mask, it would not return the correct output. * - Changed the names of public variables "day", "month", and "year" added "s" at the end for consistency purposes */ class Date_Time_Calc { /* PUBLIC VARIABLES */ public $date_time_stamp; //the date to be calculated in timestamp format public $date_time; //the date to be calculated. ex: 12/30/2008 17:40:00 public $date_time_mask; //the php date() style format that $date_time is in. ex: m/d/Y H:i:s public $seconds; public $minutes; public $hours; public $days; public $ampm; /* CONSTRUCTOR and DESTRUCTOR */ /** Constructor. This is where you supply the date. Accepts almost any format of * date as long as you supply the correct mask. DOES accept dates * without leading zeros (n,j,g,G) as long as they aren't bunched together. * ie: ("1152008", "njY") wont work; ("1/15/2008", "n/j/2008") will work. * Example: $obj = new Date_Time_Calc('12/30/2008 17:40:00', 'm/d/Y H:i:s'); */ public function __construct($start_date_time, $mask) { $this->_default_date_time_units(); //set date&time units to default values $this->date_time = $start_date_time; $this->date_time_mask = $mask; //convert date to timestamp $this->date_time_stamp = $this->_date_to_timestamp($start_date_time, $mask); } public function __destruct() { unset($this->date_time_stamp); unset($this->date_time); unset($this->date_time_mask); unset($this->seconds); unset($this->minutes); unset($this->hours); unset($this->days); unset($this->ampm); } /* PRIVATE FUNCTIONS */ /** Private Function. Resets date and time unit variables to default */ private function _default_date_time_units() { $this->seconds = '00'; $this->minutes = '00'; $this->hours = '12'; $this->days = '01'; $this->ampm = 'am'; } /** Private Function. Converts a date into a timestamp. Accepts almost any * format of date as long as you supply the correct mask. DOES accept dates * without leading zeros (n,j,g,G) as long as they aren't bunched together. * ie: ("1152008", "njY") wont work; ("1/15/2008", "n/j/2008") will work */ private function _date_to_timestamp($thedate, $mask) { $mask_orig = $mask; // define the valid values that we will use to check // value => length $all = array( //time 's' => 'ss', // Seconds, with leading zeros 'i' => 'ii', // Minutes with leading zeros 'H' => 'HH', // 24-hour format of an hour with leading zeros 'h' => 'hh', // 12-hour format of an hour with leading zeros 'G' => 'GG', // 24-hour format of an hour without leading zeros 'g' => 'gg', // 12-hour format of an hour without leading zeros 'A' => 'AA', // Uppercase Ante meridiem and Post meridiem 'a' => 'aa', // Lowercase Ante meridiem and Post meridiem //year 'y' => 'yy', // A full numeric representation of a year, 4 digits 'Y' => 'YYYY', // A two digit representation of a year //month 'm' => 'mm', // A numeric representation of a month with leading zeros. 'M' => 'MMM', // A textual representation of a month. 3 letters. ex: Jan, Feb, Mar, Apr... 'n' => 'nn', // Numeric representation of a month, without leading zeros //days 'd' => 'dd', // Day of the month, 2 digits with leading zeros 'j' => 'jj', // Day of the month without leading zeros 'S' => 'SS', // English ordinal suffix for the day of the month, 2 characters (st, nd, rd, or th. works well with j) 'D' => 'DDD' // Textual representation of day of the week (Sun, Mon, Tue, Wed) ); // this will give us a mask with full length fields $mask = str_replace(array_keys($all), $all, $mask); $vals = array(); //loop through each character of $mask starting at the beginning for ($i=0; $i<strlen($mask_orig); $i++) { //get the current character $thischar = substr($mask_orig, $i, 1); //if the character is not in the $all array, skip it if (array_key_exists($thischar, $all)) { $type = $thischar; $chars = $all[$type]; // get position of the current char if(($pos = strpos($mask, $chars)) === false) continue; // find the value from $thedate $val = substr($thedate, $pos, strlen($chars)); /* START FIX FOR UNITS WITHOUT LEADING ZEROS */ if ($type == "n" || $type == "j" || $type == "g" || $type == "G") { //if its not numeric, try a shorter digit if (!is_numeric($val)) { $val = substr($thedate, $pos, strlen($chars)-1); $mask = str_replace($chars, $type, $mask); } else { //try numeric value checking switch ($type) { case "n": if ($val > 12 || $val < 1) { //month must be between 1-12 $val = substr($thedate, $pos, strlen($chars)-1); $mask = str_replace($chars, $type, $mask); } break; case "j": if ($val > 31 || $val < 1) { //day must be between 1-31 $val = substr($thedate, $pos, strlen($chars)-1); $mask = str_replace($chars, $type, $mask); } break; case "g": if ($val > 12 || $val < 1) { //day must be between 1-12 $val = substr($thedate, $pos, strlen($chars)-1); $mask = str_replace($chars, $type, $mask); } break; case "G": if ($val > 24 || $val < 1) { //day must be between 1-24 $val = substr($thedate, $pos, strlen($chars)-1); $mask = str_replace($chars, $type, $mask); } break; } } } /* END FIX FOR UNITS WITHOUT LEADING ZEROS */ //save this value $vals[$type] = $val; } } foreach($vals as $type => $val) { switch($type) { case 's' : $this->seconds = $val; break; case 'i' : $this->minutes = $val; break; case 'H': case 'h': $this->hours = $val; break; case 'A': case 'a': $this->ampm = $val; break; case 'y': $this->years = '20'.$val; break; case 'Y': $this->years = $val; break; case 'm': $this->months = $val; break; case 'M': $this->months = $this->_month_num($val, true); break; case 'd': $this->days = $val; break; //ones without leading zeros: case 'n': $this->months = $val; break; case 'j': $this->days = $val; break; case 'g': $this->hours = $val; break; case 'G': $this->hours = $val; break; } } if (strtolower($this->ampm) == "pm") {$this->hours = $this->hours + 12;} //if its pm, add 12 hours $make_stamp = mktime( (int)$this->hours, (int)$this->minutes, (int)$this->seconds, (int)$this->months, (int)$this->days, (int)$this->years); return $make_stamp; } /** PUBLIC FUNCTIONS */ /** Add time to the date. * $what = second, minute, hour, day, week, month, or year * $only_return_the_value = set the $date_time var or just return the val. true or false * Example: $obj->calculate('sec', 90, 'add'); //adds 90 seconds (1.5 minutes) to date */ public function calculate($what, $howmuch,$raids, $add_or_sub, $only_return_the_value=false) { $new_day = $this->days; $new_hours = $this->hours; $new_minutes = $this->minutes; $new_seconds = $this->seconds; //figure out WHAT they want to add switch ($what) { case "hour": case "hours": case "hr": case "hr.": case "hrs": case "hrs.": case "h": case "h.": $timetoadd = $howmuch * 3600; //3600 seconds in an hour $method = "timestamp"; break; } if ($method == "timestamp") { //add or subtract $timetoadd to original timestamp and set or return new date if ($add_or_sub == "add") { $newtimestamp = $this->date_time_stamp + $timetoadd; } else { $newtimestamp = $this->date_time_stamp - $timetoadd; } } elseif ($method == "manual") { $newtimestamp = mktime($new_hours, $new_minutes, //create new timestamp $new_seconds, $new_month, $new_day, $new_year); } //save the values if they want them set if ($only_return_the_value == false) { $this->date_time_stamp = $newtimestamp; //update the timestamp variable for future use $this->date_time = date($this->date_time_mask, $newtimestamp); //update the formal date variable $this->years = $new_year; //update the $this->years month day hours minutes seconds vars $this->months = $new_month; $this->days = $new_day; $this->hours = $new_hours; $this->minutes = $new_minutes; $this->seconds = $new_seconds; } //return the value return date($this->date_time_mask, $newtimestamp); } /** shortcut for calculate($what, $howmuch, "add") * $what = second, minute, hour, day, week, month, or year * $only_return_the_value = set the $date_time var or just return the value. true or false * Example: $obj->add('week', 2); //adds 2 weeks (14 days) to date */ public function add($what, $howmuch, $only_return_the_value=false) { return $this->calculate($what, $howmuch, "add", $only_return_the_value); } /** shortcut for calculate($what, $howmuch, "subtract") * $what = second, minute, hour, day, week, month, or year * $only_return_the_value = set the $date_time var or just return the value. true or false * Example: $obj->subtract('hour', 48); //subtracts 48 hours (2 days) from date */ public function subtract($what, $howmuch, $only_return_the_value=false) { return $this->calculate($what, $howmuch, "subtract", $only_return_the_value); } /** Changes the date to a new one. * Example: $obj->set_date_time('11/20/2005 07:40:00 AM', 'm/d/Y H:i:s A'); */ public function set_date_time($start_date_time, $mask) { $this->__construct($start_date_time, $mask); } } ?> You can also check it online; http://l2calc.110mb.com/test.php even though some times it doesn't load, since i screwed up so much of the code just keep pressing F5 So here's the basic; time of death is the date to be filled, like it is on the example, dd Mon YYYY hh:mm respawn time is the hours to be added to that date entered previously, example: 16 That's working alright and that's what i want it to do, with one exception, i want to use the "Spawn Option" instead of having to enter the respawn time in hours, example: time of death: 12 Jul 2010 12:00 respawn time(spawn option): Value1 (which would correspond to 16 in the code, adding 16 hours to the previous date) so that there would be only; enter date and chose option, no "respawn time" option, know what i mean? I want to replace the variables with constant values, value1 = 16 value2 = 29 value3 = 281 instead of having to input the value yourself, just chose it from drop list. I've looked and tested many php drop lists but most use database, which i could use too but since i only need few limited values I'm trying to keep it simple. I'm not asking you to fix the code for me (but feel free too ;P or tweak it a bit if something is just wrong), I'm asking you to maybe link me to something that would work in this sort of code without completely messing it up or making me have to modify a whole lot. sorry for the long post Thanks in advance.
×
×
  • 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.