Vanojx1
Vanojx1

Reputation: 5574

php add x weeks to a date and then find the next given day

I can add x week to my date

//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5

$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));

now how can i give a day starting from that week example:

//$mydate + "next Monday" -----> final date

and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change

Upvotes: 0

Views: 6783

Answers (4)

Eagle Agency
Eagle Agency

Reputation: 167

I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.

I have also added in an estimated delivery which would be 6 weeks after that date.

We will be placing our next order for this on:
    <?php
    $date = '2020/05/26';
    $weeks = 2;
    $dateTime = DateTime::createFromFormat('Y/m/d', $date);
    $dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
    $dateTime->modify('wednesday');
    echo $dateTime->format('d/m/Y');
    ?>

Expected delivery for the next order will be:
    <?php
    $date = '2020/05/26';
    $weeks = 2;
    $dateTime = DateTime::createFromFormat('Y/m/d', $date);
    $dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
    $dateTime->modify('+42 days next wednesday');
    echo $dateTime->format('d/m/Y');
    ?>

If anyone can confirm this is correct that would be great.

Upvotes: 0

chocochaos
chocochaos

Reputation: 1596

I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.

For example:

// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;

// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');

// Output the new date.
echo $dateTime->format('Y-m-d');

References:

Upvotes: 3

GolezTrol
GolezTrol

Reputation: 116120

The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:

$mydate = strtotime('+3 weeks');

It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:

// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');

// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);

See strtotime documentation for more examples of notations that you can use.

Upvotes: 4

mjoschko
mjoschko

Reputation: 564

Are you looking for something like this?

$today = time();

$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));

PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).

Upvotes: 1

Related Questions