PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cale O  >  Date and Time Calculator  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Includes many date and time example calculations using this class.
Class: Date and Time Calculator
Add or subtract dates and times in custom formats
Author: By
Last change: Changed names of public variables "day", "month" "year" (added "s" at the end of each) for consistency purposes.
Date: 6 years ago
Size: 8,229 bytes
 

Contents

Class file image Download
<?php

//require class file
require_once 'class.datetimecalc.php';



/**
*
*
* There are two ways to make a calculation. You can use either calculate() or add() and subtract()
* $obj->calculate("month", 2, "add"); is the same as: $obj->add("month", 2);
* $obj->calculate("month", 2, "subtract"); is the same as: $obj->subtract("month", 2);
*
*
* When using the add() subtract() or calculate() functions, you may provide a unit of time in almost any
* form imaginable.
* Example: If you want to add 30 seconds to the date/time, you may use any of the following:
* $obj->add("second", 30);
* $obj->add("seconds", 30);
* $obj->add("sec", 30);
* $obj->add("sec.", 30);
* $obj->add("secs", 30);
* $obj->add("secs.", 30);
* $obj->add("s", 30);
* $obj->add("s.", 30);
*
*
*
*/



//
// The following is a list of example date/time calculations with this class.
// You can use many different date/time formats, you only need to provide the mask (as in the php date() function)
// Currently, d, j, S, D, m, M, n, o, Y, y, a, A, g, G, h, H, i, s are supported.
//
// Dates and times without leading zeros will also work too (example: 5/25/08 5:45pm)
//



//subtract() method:

$orig_date = "03/19/2008";
$orig_mask = "m/d/Y";
$action = "Subtract 4 months";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->subtract("m", 4);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<br>";

//outputs: 11/19/2007

echo "you can also get the units of time individually from the public variables in the object<br>";
//$obj->months $obj->days $obj->years $obj->hours $obj->minutes $obj->seconds $obj->ampm

echo " &nbsp; &nbsp; &nbsp; Month: " . $obj->months; //outputs: 11
echo "<br>";
echo
" &nbsp; &nbsp; &nbsp; Day: " . $obj->days; //outputs: 19
echo "<br>";
echo
" &nbsp; &nbsp; &nbsp; Year: " . $obj->years; //outputs: 2007

echo "<hr>";







//calculate() method:

$orig_date = "05/15/2007";
$orig_mask = "m/d/Y";
$action = "Subtract 2 months";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->calculate("m", 2, "subtract");

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 03/15/2007








//add() method:

$orig_date = "1:20 AM";
$orig_mask = "g:i A";
$action = "Add 75 minutes";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("min", 75);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 2:35 AM








$orig_date = "03:20:10 PM";
$orig_mask = "h:i:s A";
$action = "Add 100 seconds";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("sec", 100);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 03:21:50 PM









$orig_date = "23:20";
$orig_mask = "H:i";
$action = "Add 2 hours";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("h", 2);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 01:20









$orig_date = "05:25:05 PM";
$orig_mask = "h:i:s";
$action = "Subtract 300 seconds";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->subtract("sec", 300);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 05:20:05











$orig_date = "3 9 08";
$orig_mask = "n j y";
$action = "Subtract 2 weeks";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->subtract("weeks", 2);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 2 24 08









$orig_date = "Mar 5th, 2008";
$orig_mask = "M jS, Y";
$action = "Add 30 days";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("days", 30);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: Apr 4th, 2008










$orig_date = "5/18/2008";
$orig_mask = "n/j/Y";
$action = "Subtract 6 months";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->subtract("mos.", 6);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 11/18/2007











$orig_date = "Fri, Feb 9th, 2007";
$orig_mask = "D, M jS, Y";
$action = "subtract 10 days";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->subtract("day", 10);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: Tue, Jan 30th, 2007









$orig_date = "Sat, Feb 9th 5:45PM 2008";
$orig_mask = "D, M jS g:iA Y";
$action = "add 50 hours";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("hrs.", 50);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: Mon, Feb 11th 7:45PM 2008








$orig_date = "Sun Mar 03rd 2008";
$orig_mask = "D M dS Y";
$action = "add 2 days";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("days", 2);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: Wed Mar 05th 2008










$orig_date = "219";
$orig_mask = "nj";
$action = "add 1 month";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("month", 1);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 319









$orig_date = "119";
$orig_mask = "nj";
$action = "add 1 month";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->add("month", 1);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 129










$orig_date = "11/20/2005 07:40:00 PM";
$orig_mask = "m/d/Y h:i:s A";
$action = "subtract 3 years";

$obj = new Date_Time_Calc($orig_date, $orig_mask);
$obj->subtract("years", 3);

echo
"<b>Original Date/Time:</b> " . $orig_date;
echo
"<br>";
echo
"<b>Mask:</b> " . $orig_mask;
echo
"<br>";
echo
"<b>Action: </b>" . $action;
echo
"<br>";
echo
"<b>Output: </b>" . $obj->date_time;
echo
"<hr>";

//outputs: 11/20/2002 07:40:00 PM














?>