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!

1

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
For example, $pay_day=5; //1=mon... 5=fri
So I am trying to take the new date (after adding the 3 days) and finding the first Friday from that date or newer
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
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");
}

1 Comment

Brilliant, thank you. This is exactly what I need (the second section).
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.