joshi_v Posted March 28, 2007 Share Posted March 28, 2007 Hi all, Any one can suggest me how to calculate the difference between 2 dates and it shouldn't be more then 5 years. So pls tell me how to code this? Thanks in advance! Joshi. Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/ Share on other sites More sharing options...
CorfuVBProgrammer Posted March 28, 2007 Share Posted March 28, 2007 Do you want somethink like that ? ? ? <?php $date1 = date("Y"); $date2 = date("Y")+11; $deference; if($date1 > $date2) { $deference = ($date1 - $date2); } else { $deference = ($date2 - $date1); } if($deference <= 5) { echo "It's ok"; } else { echo "It's to much"; } ?> Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216603 Share on other sites More sharing options...
joshi_v Posted March 28, 2007 Author Share Posted March 28, 2007 Thanks for reply! This code will check only year part. i have to got check the whole date. Example: Current Date : 20070328 (ymd) The date i have to check is 20020327 Differnce between these 2 days id 5 years 1 day. With this result i want to check it is greater than 5 years or not. Hope i could explain better.. Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216605 Share on other sites More sharing options...
CorfuVBProgrammer Posted March 28, 2007 Share Posted March 28, 2007 the deference must be less that 5 years and 1 day ? ? ? ? For example 1/1/1900 1/1/1905 is correct (format [ day / month / year ]) 1/1/1900 2/1/1905 is incorrect (format [ day / month / year ]) Tell me to know about . . . Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216606 Share on other sites More sharing options...
joshi_v Posted March 28, 2007 Author Share Posted March 28, 2007 No..IT should be less than 5 years from the current date. Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216608 Share on other sites More sharing options...
CorfuVBProgrammer Posted March 28, 2007 Share Posted March 28, 2007 Because i can't catch what you realy need try this code <?php $datestamp1 = 20070328; $datestamp2 = 20020327; $d1_day = idate("d", $datestamp1); // Day $d1_mon = idate("m", $datestamp1); // Month $d1_yea = idate("Y", $datestamp1); // Year $d1_hou = idate("H", $datestamp1); // Hour $d1_min = idate("i", $datestamp1); // Minutes $d1_sec = idate("s", $datestamp1); // Seconds $d2_day = idate("d", $datestamp2); // Day $d2_mon = idate("m", $datestamp2); // Month $d2_yea = idate("Y", $datestamp2); // Year $d2_hou = idate("H", $datestamp2); // Hour $d2_min = idate("i", $datestamp2); // Minutes $d2_sec = idate("s", $datestamp2); // Seconds echo $d1_day . "/" . $d1_mon . "/" . $d1_yea . " - - " . $d1_hou . ":" . $d1_min . ":" . $d1_sec; echo "<br />"; echo $d2_day . "/" . $d2_mon . "/" . $d2_yea . " - - " . $d2_hou . ":" . $d2_min . ":" . $d2_sec; ?> and make tha changes as you want . . . Kind regards Nikos . . . Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216612 Share on other sites More sharing options...
jitesh Posted March 28, 2007 Share Posted March 28, 2007 echo " This is date diff between to dates in days ". date("d",strtotime('2007-03-03')-strtotime('2007-02-03')); Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216619 Share on other sites More sharing options...
CorfuVBProgrammer Posted March 28, 2007 Share Posted March 28, 2007 echo " This is date diff between to dates in days ". date("d",strtotime('2007-03-03')-strtotime('2007-02-03')); try the following idate("d",strtotime('2007-03-03')-strtotime('2007-02-03')); Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216621 Share on other sites More sharing options...
joshi_v Posted March 28, 2007 Author Share Posted March 28, 2007 Thank u all guys for your valuable suggestions! I could do this in my own way atlast! Actually what i am trying to do is, checking the given date should be in the time span of 5 years from the current date. The difference between the current date and the given date should be less than 5 years! Here is my code for it. function check_date($paramDate) (Y-m-d) Format. { $today_date=date('Y-m-d'); #Current Date. $temp_year=substr($today_date,0,4)-5; # Subtract 5 years the from the current year. $compare_with=$temp_year.'-'.substr($today_date,5,2).'-'.substr($today_date,8,2); # Form another date with the previous year. 5years back date for today's date. if(strtotime($paramDate)<strtotime($compare_with)) { return false; } else { return true; } } Once Again thank u Nikos and Jitesh for spending u r valuable time on this. Have a great Day!! Joshi. Link to comment https://forums.phpfreaks.com/topic/44596-solved-how-to-calculate-date-difference/#findComment-216638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.