Date/Time Calculator Class Usage/Examples


Remember, you can add and subtract from almost any form of date/time. If you want to add seconds for example, it accepts almost any form of the word second: second, seconds, sec, secs, s. Thats the same for all the other units of time also. It will calculate leap years, different number of days in each month, it will roll over to the next year or month if necessary. It also accepts date/times that don't have leading zeros (such as n/j/y). It works fine as long as the numbers aren't bunched together like: 11908 (njy), (not even a human can read that), but 11 9 08 or 11/9/08 or 11/9 08 will work.

Original Date Mask Calculation Output Syntax
11/1/2008 17:40:00 n/j/Y H:i:s add 15 months 2/1/2010 17:40:00 $obj = new Date_Time_Calc("11/1/2008 17:40:00", "n/j/Y H:i:s");
echo $obj->add("mo", 15);     //you may echo the return value of add() or subtract()
11/20/2005 07:40:00 PM m/d/Y h:i:s A subtract 36 hours 11/22/2005 07:40:00 AM $obj = new Date_Time_Calc("11/20/2005 07:40:00 PM", "m/d/Y h:i:s A");
$obj->subtract("hours", 36);
echo $obj->date_time;   //or you can call add() or subtract() and echo the public $date_time variable
11/20/2005 07:40:00 PM m/d/Y h:i:s A subtract 36 hours 11/19/2005 07:40:00 AM $obj = new Date_Time_Calc("11/20/2005 07:40:00 PM", "m/d/Y h:i:s A");
$obj->calculate("h", 36, "subtract"); //you can use calculate() instead of add() or subtract()
echo $obj->date_time;
02/25/2008 m/d/Y add 10 days 03/06/2008 $obj = new Date_Time_Calc("02/25/2008", "m/d/Y");
$obj->add("days", 10);
echo $obj->date_time;   //it even knows leap years too!
3/19/1985 n/j/Y subtract 90 days 12/19/1984
3/19/1985

$obj = new Date_Time_Calc("3/19/1985", "n/j/Y");
echo $obj->calculate("d", 90, "sub", true); //only returns the value, doesnt set $date_time var.
echo "<br>" . $obj->date_time;

3/19/1985 7:35 AM n/j/Y g:i A add 50,000 minutes 4/23/1985 12:55 AM

$obj = new Date_Time_Calc("3/19/1985 7:35 AM", "n/j/Y g:i A");
$obj->calculate("mins", 50000, "add"); //returns the result and sets the $date_time var.
echo $obj->date_time;

Dec 5th, '08 08:05 am M jS, 'y h:i a add 90 minutes
add 3 months
add 1 year
Mar 5th, '10 09:35 am

$obj = new Date_Time_Calc("Dec 5th, '08 08:05 am", "M jS, 'y h:i a");
$obj->add("i", 90);     //you may do multiple calculations one after another
$obj->add("m", 3);
$obj->add("y", 1);
echo $obj->date_time;

219 nj add 1 month 319 $obj = new Date_Time_Calc("219", "nj");   //its smart... it knows there's no such month as 21, so it must be 2
echo $obj->add("month", 1);
119 nj add 1 month 129 $obj = new Date_Time_Calc("119", "nj");    //this defaults to November 9th, not January 19th.
echo $obj->add("month", 1);
11 20 2005 07:40:25 PM m/d/Y h:i:s A

subtract 120 seconds
add 60 seconds
add 20 seconds
add 10 seconds
add 15 seconds

11/20/2005 07:40:10 PM $obj = new Date_Time_Calc("11 20 2005 07:40:25 PM", "m/d/Y h:i:s A");
$obj->subtract("secs", 120);       //you can use: second, seconds, secs, sec, or s to calculate seconds
$obj->add("sec", 60);
$obj->add("s", 20);
$obj->add("second", 10);
$obj->add("seconds", 15);
echo $obj->date_time;
Fri, Feb 9th, 2007 D, M jS, Y subtract 10 days Tue, Jan 30th, 2007 $obj = new Date_Time_Calc("Fri, Feb 9th, 2007 ", "D, M jS, Y");
$obj->subtract("days", 10);       //you may also provide the day of the week in "D" format (Sun, Mon, Tue)
11:20 AM g:i A add 2 hours
add 75 minutes
2:35 PM

$obj = new Date_Time_Calc("11:20 AM", "g:i A");       //time only
$obj->add("hour", 2);
$obj->add("min", 75);
echo $obj->date_time;