ramli Posted July 19, 2007 Share Posted July 19, 2007 I created a function to show me a date after i did some calculations with it. it works to show singel varables for exaple: $date1 = "2007-06-16"; $date2 = "2007-06-18"; $date1 = strtotime($date1); $date2 = strtotime($date2); $calc = $date2 - $date1; $dateview = TCONV($calc); echo($dateview['day']); This returns 2 days differance however when i want to calculate with a date i get strange readings $date1 = "2007-06-16"; $date1 = strtotime($date1); $date1 = $date1 + 86400; // for 1 day $dateview = TCONV($date1); echo($dateview['day'].$dateview['month'].$dateview['year']); in this case it returns 13680.91666666775771.2307692316314.2692307692 with i dont understand wy. It should have to return 2007-06-17. function is below //--------------------------TCONV----------------------------// //Get date from seconds function TCONV($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); } if(isset($year)){$time_int['year'] = $year;} if(isset($month)){$time_int['month'] = $month;} if(isset($day)){$time_int['day'] = $day;} if(isset($hour)){$time_int['hour'] = $hour;} if(isset($minute)){$time_int['minute'] = $minute;} if(isset($seconds)){$time_int['seconds'] = $seconds;} return $time_int; } I have had some problems with this bevore can somone please help me with this ? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 19, 2007 Share Posted July 19, 2007 What is it that you are trying to do exactly? What is the purpose of the function? It seems to me like what you are trying to achieve can be done much more easily with the date() function. Quote Link to comment Share on other sites More sharing options...
ramli Posted July 19, 2007 Author Share Posted July 19, 2007 i need to be able to calculate with dates like 2007-04-18 - 2007-04-16 to be 2 (days). To achieve this i strtotime my variables so ik kan add them to echother however i alsow want to be able to add like in my exaple 2007-04-16 + 1 day is 2007-04-17. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 19, 2007 Share Posted July 19, 2007 A combination of the sttotime() and date() functions should be able to do all that for you. Quote Link to comment Share on other sites More sharing options...
ramli Posted July 19, 2007 Author Share Posted July 19, 2007 could you please give me a example of how this is done ? Quote Link to comment Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 <?php /* Date Difference Calculation */ $date1 = "2007-04-18"; $date2 = "2007-04-16"; $difference = floor((strtotime($date1) - strtotime($date2)) / 24 / 60 / 60); echo "The difference in days between {$date1} and {$date2} is {$difference} days.<br />"; /* Adding Days to a Date */ $date = "2007-04-16"; $days = 1; // Days to add to the date $newdate = date("Y-m-d", (strtotime($date) + ($days * 24 * 60 * 60))); echo "{$date} + {$days} day(s) = {$newdate}"; ?> Hope that helps Quote Link to comment Share on other sites More sharing options...
ramli Posted July 19, 2007 Author Share Posted July 19, 2007 Your way works perfectly it isnt variable for exaple if i have to add a year it wouldt work. And it needs to be in a function so that in will be quickly accessible. You have alredy helped me alot but if you have any thoughts about a function i would gladly hear it. Quote Link to comment Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 <?php /* Date Difference Calculation */ function datediff($date1, $date2) { return floor((strtotime($date1) - strtotime($date2)) / 24 / 60 / 60); } echo "The difference in days between 2007-04-18 and 2007-04-16 is ".datediff("2007-04-18", "2007-04-16")." days.<br />"; /* Adding Days to a Date */ function dateadd($date, $days, $format) { return date($format, (strtotime($date) + ($days * 24 * 60 * 60))); } echo "2007-04-11 + 1 day = ".dateadd("2007-04-11", 1, "Y-m-d"); ?> EDIT: Forgot to mention, the last argument of dateadd() allows you to pick the format of the returned date. Quote Link to comment Share on other sites More sharing options...
ramli Posted July 19, 2007 Author Share Posted July 19, 2007 Thx for the evort i can alredy use your functions. Am i correct i assuming this only works for processing action concerning day's. is is possible to edit this functions to include month,years,minutes,seconds and hours. I would do it myself if i knew how but im quite new at this date thing.. Quote Link to comment Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 You came for help, you got it... lots of it. The idea of this board is to fix broken code, not for us to code your entire script for you! The basis of the code is there, just read up on it and have a go yourself Quote Link to comment Share on other sites More sharing options...
ramli Posted July 20, 2007 Author Share Posted July 20, 2007 Yes and i apreciate all your help and that of others. however if i could simply figure it myself i would not think of asking any of the forum members. Better yet i would make it and post it for future reference for myself and all other forum members. And im not demanding a script just requesting help on making one. If you dont want to create it berhaps a other member will. And if you find it a incorrect question please report it to the moderators. If the decide it is incorret than it is my bad. Thanks for your help again. 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.