简体   繁体   中英

recurring date functions in php

Good Day,

I am trying to create a recurring date system that has the following:

nth day of nth month (2nd day of every 3rd month)

$this_months_friday = strtotime('+3 days +4 months');

the output of that will always be current day + 3 days of the 4th month.

how do I get it to display the nth day of the nth month?

since i also tried

$this_months_friday = strtotime('every 3 days +4 months');

and it did not return any result. Should i stick with strtotime on this one or move to DateTime function of php. though i wont still be able to formulate the proper argument for that kind of date sequence.

Any help would be greatly appreciated.

Thank You.

Probably better off using DateTime with a couple intervals:

$d = new DateTime();
$d->add(new DateInterVal('P' . $days . 'D'))->add('new DateInterVal('P' . $months . 'M'));

not sure what youre two example intervals are wanting.

You want an internval to start in 4 months, which then repeats every 3 days?

That'd be something more like

$d = new DateTime();
$d->add(new DateInterval('P4M')); // jump ahead 4 months immediately
$day3 = new DateInterval('P3D');

for ($i = 0; $i < 100; $i++) {
   $d->add($day3); // jump ahead 3 days
   ... do something with this new date
}

for a basic recurring event, +4 months + 3 days, you'd simply have one interval:

$interval = new DateInteval('P4M3D'); // +4 months +3 days
$date = new DateTime();
while($some_condition) {
   $date->add($interval);
   do_something();
}

You can do this by saving the values in variables like that :

    $day=3;
    $month=4;
    echo date("d-m-y",strtotime('+'.$day .'days' .'+'.$month.'months'));

Explanation:

7 (july)+ 4 months = 11 month(November)

8 july+ 3 days = 11 july

Output: 11-11-13

NOTE: just for the example I have put the values hard coded, You can make them dynamic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM