UnsuitableBadger Posted August 24, 2009 Share Posted August 24, 2009 Hi everyone, I'm having trouble with my code: <?php $startDate = date("2009-08-01"); $endDate = date("2009-10-30"); $todaysDate = date("Y-m-d"); $recurrenceType = 1; $recurrenceNumber = 1; for ($loopDate = $startDate; $loopDate <= $endDate; $loopDate = date("Y-m-d", strtotime($loopDate)+86400)) { $count++; // Daily if ($recurrenceType == 1) { if ($recurrenceNumber == 1) echo $loopDate."<br />"; else { if (($count % $recurrenceNumber) == 1) { echo $loopDate."<br />"; } } } // Weekly elseif ($recurrenceType == 2) { if (($count % (7 * $recurrenceNumber)) == 1) { echo $loopDate."<br />"; } } // Monthly elseif ($recurrenceType == 3) { if (($count % (30 * $recurrenceNumber)) == 1) { echo $loopDate."<br />"; } } } ?> This code basically runs from the $startDate to the $endDate and will list the dates as follows: if $recurrenceType = 1 it will list dates daily if $recurrenceType = 2 it will list dates weekly if $recurrenceType = 3 it will list dates monthly while $recurrenceNumber will list the recurring spacer so if you set $recurrenceType = 1 and $recurrenceNumber = 2 then the program will display every second date from start to end dates. My problem is that if I start the date at 2009-08-01 and set the end date to 2009-10-25 or above it gets stuck on 2009-10-25 and just keeps spitting that date out. Can anyone shed some light on why this would be happening? Thanks Link to comment https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/ Share on other sites More sharing options...
deansatch Posted August 24, 2009 Share Posted August 24, 2009 The problem is where you are adding your extra day. It is giving the 1970 result. Try this: <?php $startDate = strtotime(date("2009-08-01")); $endDate = strtotime(date("2009-10-30")); $todaysDate = date("Y-m-d"); $recurrenceType = 1; $recurrenceNumber = 1; for ($loopDate = $startDate; $loopDate <= $endDate; $loopDate = mktime(0,0,0,date("m",$loopDate),date("d",$loopDate)+1,date("Y",$loopDate))) { $count++; // Daily if ($recurrenceType == 1) { if ($recurrenceNumber == 1) echo date('Y-m-d',$loopDate)."<br />"; else { if (($count % $recurrenceNumber) == 1) { echo date('Y-m-d',$loopDate)."<br />"; } } } // Weekly elseif ($recurrenceType == 2) { if (($count % (7 * $recurrenceNumber)) == 1) { echo date('Y-m-d',$loopDate)."<br />"; } } // Monthly elseif ($recurrenceType == 3) { if (($count % (30 * $recurrenceNumber)) == 1) { echo date('Y-m-d',$loopDate)."<br />"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/#findComment-904926 Share on other sites More sharing options...
UnsuitableBadger Posted August 24, 2009 Author Share Posted August 24, 2009 Could you maybe suggest a way i could solve this as i'm quite unsure. I tried checking the date at every stage and i don't seem to get any returns of 1970. Link to comment https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/#findComment-904942 Share on other sites More sharing options...
deansatch Posted August 24, 2009 Share Posted August 24, 2009 Could you maybe suggest a way i could solve this as i'm quite unsure. I did. In fact I rewrote a full working version in my last post Link to comment https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/#findComment-905112 Share on other sites More sharing options...
kenrbnsn Posted August 24, 2009 Share Posted August 24, 2009 You're comparing dates which won't work. This works fine: <?php $startDate = strtotime("2009-08-01"); $endDate = strtotime("2009-10-30"); $todaysDate = time(); $recurrenceType = (isset($_GET['rt']))?$_GET['rt']:1; $recurrenceNumber = (isset($_GET['rn']))?$_GET['rn']:1; for ($loopDate = $startDate; $loopDate <= $endDate; $loopDate += 86400) { $count++; // Daily if ($recurrenceType == 1) { if ($recurrenceNumber == 1) echo date('Y-m-d',$loopDate)."<br />"; else { if (($count % $recurrenceNumber) == 1) { echo date('Y-m-d',$loopDate)."<br />"; } } } // Weekly elseif ($recurrenceType == 2) { if (($count % (7 * $recurrenceNumber)) == 1) { echo date('Y-m-d',$loopDate)."<br />"; } } // Monthly elseif ($recurrenceType == 3) { if (($count % (30 * $recurrenceNumber)) == 1) { echo date('Y-m-d',$loopDate)."<br />"; } } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/#findComment-905157 Share on other sites More sharing options...
UnsuitableBadger Posted August 25, 2009 Author Share Posted August 25, 2009 Sorry my attention span was dwindling yesterday Thanks for the help I appreciate it! Link to comment https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/#findComment-905620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.