Sean Kimball
Sean Kimball

Reputation: 4494

How to find a certain date after another given date in using php?

How can I find the next occurrence of a fixed date after another date?

For example:

I need to find when April 1 is

if the date I have is 2013-02-28 I need to return 2013-04-01

if the date I have is 2013-05-01 I need to return 2014-04-01

Is there a quick way to do this?

update

Relative date formats do not work correctly in php 5.3.3 This is what I would up doing instead:

function aprilFoolsDay($datein,$format){

    $date = new DateTime($datein);

    $year = $date->format("Y");

    $april = new DateTime($year.'-04-01');

    if($date > $april){

        $april = new DateTime(($year + 1).'-04-01');

    }

    return $april->format($format);

}


echo aprilFoolsDay('2011-05-28','Y-m-d');

Upvotes: 1

Views: 127

Answers (4)

John Conde
John Conde

Reputation: 219874

You can use relative time formats to get to the date you seek:

$date1 = new DateTime('2013-02-28');
$date2 = clone $date1;
$date2->modify('First day of  April');
if ($date2 < $date1) {
    $date2->modify('+1 year');
}
echo $date2->format('Y-m-d');

$date1 = new DateTime('2014-05-01');
$date2 = clone $date1;
$date2->modify('First day of  April');
if ($date2 < $date1) {
    $date2->modify('+1 year');
}
echo $date2->format('Y-m-d');

Output:

2013-04-01
2015-04-01

Here's a reusable function:

echo getNextAprilFirst('2013-02-28');
echo getNextAprilFirst('2014-05-01');

function getNextAprilFirst($date) {
    $date1 = new DateTime($date);
    $date2 = clone $date1;
    $date2->modify('First day of  April');
    if ($date2 < $date1) {
        $date2->modify('+1 year');
    }
    return $date2->format('Y-m-d');
}

Output:

2013-04-01
2015-04-01

See it in action

Upvotes: 4

Joshua Bixler
Joshua Bixler

Reputation: 531

<?php
/**
 * @param $date string of date
 * @return unix time of april fools
 */
function getNextAprilFoolsOfYear($date) {
    // unix time for date
    $unixdate = strtotime($date);

    $year = date('Y', $unixdate);

    $currentyearfools = strtotime($year . '-04-01');

    // check if current date is past this year april fools or not
    if ($unixdate < $currentyearfools) {
        // if not pass yet
        return $currentyearfools;
    } else {
        return strtotime(($year + 1) . '-04-01');
    }
}

echo date('Y-m-d', getAprilFoolsOfYear('2014-01-01'));
echo '<br />';
echo date('Y-m-d', getNextAprilFoolsOfYear('2014-05-01'));

Output:

2014-04-01
2015-04-01

Upvotes: 2

bansi
bansi

Reputation: 57042

you can do it with strtotime. Note this function is timezone aware.

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

$dtstr = '2013-03-28';
$dtmonth = date("m",strtotime($dtstr));
$dtyear = date("Y",strtotime($dtstr));
$dtyear = $dtmonth<4?$dtyear:$dtyear+1;
$dt = strtotime('1 April ' . $dtyear);
echo date("Y-m-d",$dt);

Upvotes: 1

CrayonViolent
CrayonViolent

Reputation: 32517

I don't think Relative Format supports something like "next april" (this example certainly doesn't work), but you can do this:

$date = new DateTime("2013-05-01");
$target_date = new DateTime("April 1");
$date->modify(
  $target_date->format("m/d"). 
  (($date->format('m')>$target_date->format('m'))?"+1 year":'')
);
echo $date->format("Y-m-d"); // output: 2014-04-01

Upvotes: 2

Related Questions