2

Can anyone help create this logic? Kind of difficult to explain...

Looking to take a date, add 3 days then select the next date based on a database value.

Say we start with:

$end_date = "2017-08-23 23:59:59"
$payday = 5; //1=monday, 2=tuesday, 3=wednesday, 4=thursday, 5=friday
//And we want to calculate $paydate:
$temp_date = $end_date + 3 days;
$pay_date = the first $payday(day of week) after $temp_date

Any ideas how to write this in php? This one is stumping me. Thanks!

ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
Jeff Sayers
  • 117
  • 1
  • 9
  • this should get you most of the way: https://stackoverflow.com/questions/25379449/php-first-monday-after-certain-date-as-string –  Aug 29 '17 at 01:57

4 Answers4

1

To add three days you can do this:

$date = new DateTime('2017-08-23 23:59:59');
$date->add(new DateInterval('P3D'));
$date->modify("next friday");
echo $date->format('Y-m-d') . "\n";

You could also use a lookup table, or array, that matches number to named days of the week and use something like $date->modify("next $days[$payday]"); where

$days = [ [1] => "monday",
           .... etc
ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
  • Thank you. After adding 3 days I am trying to take that date and find the next calendar day that corresponds with the 'pay_day' setting. – Jeff Sayers Aug 29 '17 at 02:07
  • For example, $pay_day=5; //1=mon... 5=fri – Jeff Sayers Aug 29 '17 at 02:08
  • So I am trying to take the new date (after adding the 3 days) and finding the first Friday from that date or newer – Jeff Sayers Aug 29 '17 at 02:09
  • assuming payday will be every week you could just use `$date->modify('next friday');` and you could use a lookup table to figure out which next day you need to use – ElefantPhace Aug 29 '17 at 02:11
0

Jeff, take a look at Carbon which extends PHP's DateTime object.

It allows you to deal with dates in a very clean and intuitive way. Based on your example:

$date = Carbon::parse('next monday')->toDateString();

Adjust that to your exact case.

Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92
0

How about this? It will get a date, add 3 days, and then loop through the next days until it finds a day in the $days array (which you can get from a database):

<?php
$date = new DateTime('2017-08-23 23:59:59');
function nextPayday($date) {
    $date->add(new DateInterval('P3D'));
    echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
    $payDate = $date->add(new DateInterval('P1D'));
    $days = ["1", "2", "3", "4", "5"];
    while (!in_array($payDate->format("N"), $days)) {
        $payDate->add(new DateInterval('P1D'));
    }
    return $payDate->format("D Y-m-d");
}
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
echo "Next payday: ".nextPayday($date);

Demo

Or, if you need to find a next specific day, use this function instead:

function nextPayday($date) {
    $date->add(new DateInterval('P3D'));
    echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
    $payDate = $date->add(new DateInterval('P1D'));
    $day = "5";
    while ($payDate->format("N") !== $day) {
        $payDate->add(new DateInterval('P1D'));
    }
    return $payDate->format("D Y-m-d");
}
ishegg
  • 9,685
  • 3
  • 16
  • 31
0

Another version to achieve the objective:

function nextPayday($dateString, $paydayNum) {
    $paydays = [
            1=>'monday',
            2=>'tuesday',
            3=>'wednesday',
            4=>'thursday',
            5=>'friday'
    ];
    $temp_date_stamp = date('d-m-Y H:i:s', strtotime($dateString.' +3 days'));
    $pay_date_stamp  = strtotime('first '.$paydays[$paydayNum].' '.$temp_date_stamp);

    return date('d-m-Y H:i:s', $pay_date_stamp);
}

$end_date = "2017-08-23 23:59:59";
$payday = 5;

echo nextPayday($end_date, $payday);

result:

01-09-2017 23:59:59
Wizard
  • 862
  • 6
  • 9