PHP classes: Date Object (Add/Subtract dates)

While coding a recent project I was looking around for a good way to simplify adding and subtracting dates. I found a function here written by “jm AT trinitywebdev DOT com”. I modified this function slightly (formatting, added support for weeks) and built a class around it. You can download the class here.

Example Usage:

require "c_date.php";	
$date = new date();

echo "Yesterday: " 	. $date->yesterday;
echo "Today: " 		. $date->today;
echo "Tomorrow: " 	. $date->tomorrow;
echo "Today minus 1 month: " 	. $date->modify($date->today,'-1m','m/d/Y');
echo "Today plus 5 days: " 		. $date->modify($date->today,'+5d','m/d/Y');
echo "Yesterday plus 6 weeks: " . $date->modify($date->yesterday,'+6w','m/d/Y');


(created on 4/19/2010)

Yesterday: 04/18/2010
Today: 04/19/2010
Tomorrow: 04/20/2010

Today minus 1 month: 03/19/2010
Today plus 5 days: 04/24/2010
Yesterday plus 6 weeks: 05/30/2010