Jump to content

[SOLVED] Calculating time function


ramli

Recommended Posts

Hi im working on a invoice system. In this system i have to calculate with time and dates.Im searching to find a custom or php function that allows me to do this. it has t work somthing like this.

 

Adding Time

2007-06-14 + 2 days = 2007-06-16

2007-06-31 + 2 days = 2007-07-02

2007-06-11 + 1 month = 2007-07-11

2007-06-11 + 1 year = 2008-06-11

ect..

 

Telling Difference

2007-06-14 ~ 2007-06-16 = 2 Days

ect..

 

Deducting Time (-)

Same story for Time (00:00:00)

 

I cant seem to find a topic or function that does all this... Perhaps im looking in the wrong places if u can help me please do so. thanks in Advance.

 

 

Link to comment
Share on other sites

Just an idea to save you some time.. you could try use a timestamp instead of actual dates... and then have a converter that changes the timestamp to a date format..

 

Then when you want to add or remove the dates you just find a function (Cause there are a good few out there) that would timestamp1 - timestamp2 = difference... and then check the difference.. if its over however many seconds there is in one day and so on..

 

Andy

 

EDIT: Oh.. beaten to the mark! Sorry

Link to comment
Share on other sites

if used this method

 

$date1 = "2007-06-16";
$date2 = "2007-06-18";

$covertdate1 = strtotime($date1);
$covertdate2 = strtotime($date2);


$differance = $covertdate2 - $covertdate1;

$result =  date("d-m-Y", $differance);

echo($result);

 

It outputs 03-01-1970 i want it to output 2 as if the difference is 2 day's what am i doing wrong ?

Link to comment
Share on other sites

 

$date1 = mktime(0,0,0, 6, 16, 2007);

$date2 = mktime(0,0,0, 6, 18, 2007);

 

$dif = $date2 - $date1;

 

You now have the SECONDS between the two dates.

 

Do some math and you can figure out how many years/months/days/hours/minutes/seconds etc are left.

Link to comment
Share on other sites

Created a function that works

 

//--------------------------TCALC----------------------------//
//Get date from seconds
function TCALC($dateinsecs)
{

  if($dateinsecs >= 187200) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $year = ($dateinsecs / 187200); 
  } 
  
  if($dateinsecs >= 15600) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $month = ($dateinsecs / 15600); 
  } 
  
  if($dateinsecs >= 86400) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $day = ($dateinsecs / 86400); 
  } 
  
  if($dateinsecs >= 3600) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $hour = ($dateinsecs / 3600); 
  }
  
  if($dateinsecs >= 60) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $minute = ($dateinsecs / 60); 
  }
  
  if($dateinsecs >= 0) 
  { 
  	$seconds = floor($dateinsecs);
  }
  
  $time_int = array('year'=>$year,'month'=>$month,'day'=>$day,'hour'=>$hour,'minute'=>$minute,'seconds'=>$seconds); 
  return $time_int;
}

 

$date1 = "2007-06-16";
$date2 = "2007-06-18";

$date1 = strtotime($date1);
$date2 = strtotime($date2);

$date = $date2 - $date1;
$dateview = TCALC($date);
echo($dateview['day']);

 

You know you will be returnd a day becouse you Deduct the dates from eachother

it will return 2 (days)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.