ramli Posted July 18, 2007 Share Posted July 18, 2007 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. Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 18, 2007 Share Posted July 18, 2007 take a look at these functions time() // Gets current timestamp mktime() // Gets timestamp of given time based on paremters strtotime() // returns timestamp of passed string ie "5 days ago" Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 18, 2007 Share Posted July 18, 2007 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 Quote Link to comment Share on other sites More sharing options...
ramli Posted July 18, 2007 Author Share Posted July 18, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 18, 2007 Share Posted July 18, 2007 $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. Quote Link to comment Share on other sites More sharing options...
ramli Posted July 18, 2007 Author Share Posted July 18, 2007 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.