jjgilels Posted August 8, 2010 Share Posted August 8, 2010 Start with: Saturday August 13 1978. What is the next year that August 13th falls on a Saturday? Couldn't find an answer to that anywhere! Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/ Share on other sites More sharing options...
jcbones Posted August 8, 2010 Share Posted August 8, 2010 August 13, 1978 was a Sunday. This will get the info, that the next time August 13 happens on Sunday is in 1989. There may be a better way, but this is what I came up with. <?php $date = new DateTime('1978-08-13'); $findDate = $date->format('l'); $interval = $date->add(new DateInterval('P1Y')); while($date->format('l') != $findDate) { $date->add(new DateInterval('P1Y')); } echo $date->format('l F d, Y'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096771 Share on other sites More sharing options...
jjgilels Posted August 9, 2010 Author Share Posted August 9, 2010 Where did you run that code? On my machine, I get this error: Fatal error: Call to undefined method DateTime::add() Jerry. Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096791 Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 http://www.php.net/manual/en/datetime.installation.php Note: Experimental DateTime support in PHP 5.1.x Although the DateTime class (and related functions) are enabled by default since PHP 5.2.0, it is possible to add experimental support into PHP 5.1.x by using the following flag before configure/compile: CFLAGS=-DEXPERIMENTAL_DATE_SUPPORT=1 Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096793 Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 I did it both ways. So you could use either one. <?php //Object Oriented. echo 'Object Oriented.<br />'; $date = new DateTime('1978-08-13'); $findDate = $date->format('l'); $interval = $date->add(new DateInterval('P1Y')); while($date->format('l') != $findDate) { $date->add(new DateInterval('P1Y')); } echo $date->format('l F d, Y'); //Procedural echo '<br /><br />Procedural<br/>'; $date = strtotime('1978-08-13'); $findDate = date('l',$date); $interval = strtotime('+1 Year',$date); $current = date('l',$interval); while($current != $findDate) { $interval = strtotime('+1 Year',$interval); $current = date('l',$interval); } echo date('l F d, Y',$interval); ?> Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096795 Share on other sites More sharing options...
jjgilels Posted August 9, 2010 Author Share Posted August 9, 2010 OK. The second one works. So, how can I get the NEXT result after today for that 1978 date? Logically, the next time Aug 13 appears on a Sunday after today. This is actually what I'm after; not the one in 1989. Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096799 Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 Try this: $month = '08'; $day = '13'; $year = '1978'; $date = strtotime($year.'-'.$month.'-'.$day); $findDate = date('l',$date); $interval = strtotime('+1 Year',strtotime(date('Y').'-'.$month.'-'.$day)); $current = date('l',$interval); while($current != $findDate) { $interval = strtotime('+1 Year',$interval); $current = date('l',$interval); } echo date('l F d, Y',$interval); Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096815 Share on other sites More sharing options...
jjgilels Posted August 9, 2010 Author Share Posted August 9, 2010 Excellent. Perfect. Brilliant. Suh-weeeet! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210140-how-do-you-find-the-next-year-a-day-falls-on-a-date/#findComment-1096822 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.