Jump to content

How Do You Find The NEXT Year A Day Falls On A Date?


jjgilels

Recommended Posts

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');
?>

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

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);
?>

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.