A PHP class for manipulation of dates

The following PHP code includes a simple class that can manipulate dates, such as date subtraction, adding days and subtracting days to the current date. It is very useful because my PHP server does not support the latest DateTime function yet.

<?php

class Dt
{
	private $day;
	private $month;
	private $year;
	private $seconds=86400;

	function __construct()
	  {
		$this->day=date("j");
		$this->month=date("n");
		$this->year=date("Y");
	  }

	function now_diff($by,$bm,$bd)
	   {
		$bm=preg_replace("/[0](.+)/is","\\1",$bm);
		$bd=preg_replace("/[0](.+)/is","\\1",$bd);
		$bt=mktime(0,0,0,$bm,$bd,$by);
		$nt=mktime(0,0,0,$this->month,$this->day,$this->year);
		$df=$nt-$bt;
		$dn=$df/$this->seconds;
		return $dn;
	  }

	function date_diff($by,$bm,$bd,$ty,$tm,$td)
	   {
		$bm=preg_replace("/[0](.+)/is","\\1",$bm);
		$bd=preg_replace("/[0](.+)/is","\\1",$bd);
		$bt=mktime(0,0,0,$bm,$bd,$by);
		$nt=mktime(0,0,0,$tm,$td,$ty);
		$df=$nt-$bt;
		$dn=$df/$this->seconds;
		return $dn;
	  }

	function earlydate($ndays)
	  {
		$nt=mktime(0,0,0,$this->month,$this->day,$this->year);
		$ds=$ndays*$this->seconds;
		return $nt-$ds;
	  }

	function latedate($ndays)
	  {
		$nt=mktime(0,0,0,$this->month,$this->day,$this->year);
		$ds=$ndays*$this->seconds;
		return $nt+$ds;
	  }

}

?>
  • Share/Bookmark

Leave a Reply

You must be logged in to post a comment.