retro Posted June 12, 2008 Share Posted June 12, 2008 Apologies if there is a glaringly obvious answer to this - I am still a relative beginner with PHP! I have used DATEDIFF in a MySQL query to determine when a date in a db (`date`) falls on the same day as an entered date ($DelDate), but they have to be fortnightly ranges. The query used: DATEDIFF(date, '$DelDate')%14=0 This will show fortnightly results, e.g. if `date` was 2008-06-12 (Thursday 12 June) then when $DelDate is 2008-06-12 or 2008-06-26, it would return results, but not if it were 2008-06-19. I now want to be able to do the same thing in PHP - determine whether a date falls on a fortnightly range of another date. I don't know whether it would help, but you could see it as the original date being on an A week, with the weeks alternating A or B, and so I want to determine whether a date is in an A week. (Incidentally, I have already filtered all results to be on that day of the week, so the rest of the week is irrelevant). Can anyone suggest anything? Thanks in advance for any information offered - it is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 12, 2008 Share Posted June 12, 2008 <?php /* dateDiff Takes the date you enter in YYYY-MM-DD format, and calcuates its relationship to the 2nd date entered. */ function dateDiff($endDate, $beginDate) { $date_parts1=explode("-", $beginDate); $date_parts2=explode("-", $endDate); $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]); $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]); return $end_date - $start_date; } /* Change these variables */ $day1 = "2008-3-1"; $day2 = "2008-3-5"; /*modify this to suit your needs */ if (dateDiff($day2, $day1) <=7){ print "first week"; } elseif (dateDiff($day2, $day1) >7 && dateDiff($day2, $day1) <=14){ print "within 2nd week"; } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 12, 2008 Share Posted June 12, 2008 Or just: function datediff($date1,$date2){ if((strtotime($date1)-strtotime($date2)) %(60*60*24*14) ==0){ return true; }else{ return false; } } Quote Link to comment Share on other sites More sharing options...
retro Posted June 12, 2008 Author Share Posted June 12, 2008 That's perfect, thanks!! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 12, 2008 Share Posted June 12, 2008 Or just: function datediff($date1,$date2){ if((strtotime($date1)-strtotime($date2)) %(60*60*24*14) ==0){ return true; }else{ return false; } } I was wondering why you did your script your way. I ran both our scripts through a while loop (1000 runs through) and mine took 0.189803 seconds to run it, while yours took 1.061343 to run it. Is there any reason to take the more time-intensive approach? sorry for questioning it, but I keep on getting corrected with my approach, and I figured I'd ask. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 13, 2008 Share Posted June 13, 2008 Oh no, there's no doubt that strtotime() has an overhead associated with it. It's just hugely convenient and generally produces shorter, easier to understand code. However, if you were intending to run this function hundreds of times is your script, then it would be worth using your method as it is quicker. I was quite suprised at how much quicker. And there's nothing wrong with questioning something -- it's the way people learn isn't it? 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.