Add parameter to function and change output

Hi all

I have a couple of functions below which basically show the day or amount of days based on the date of an event, big thanks to @ScallioXTX for helping me with this in a previous thread.

I wanted to combine these into a single function due to most of the code being the same, just a small change to one of the cases inside the switch.

ScallioXTX suggested:

You could add a parameter to the function and depending on the value of that parameter change the output.

function dateString(DateTime $dt) {
    $dt->modify('midnight');
    $now = new DateTime('midnight');
    $diff = $now->diff($dt);
    $days = $diff->days;
    if ($now > $dt) {
        $days *= -1;
    }

    switch (true) {
        case $days === 0:
            return 'Today';
        case $days === 1:
            return 'Tomorrow';
        case $days === -1:
            return 'Yesterday';
        case $days < -1:
            return abs($days).' days ago';
        default:
            return $dt->format('l, j F');
    }
}
function dateString2(DateTime $dt) {
    $dt->modify('midnight');
    $now = new DateTime('midnight');
    $diff = $now->diff($dt);
    $days = $diff->days;
    if ($now > $dt) {
        $days *= -1;
    }

    switch (true) {
        case $days === 0:
            return 'today';
        case $days === 1:
            return 'tomorrow';
        case $days > 1:
            return $days.' days';
        case $days === -1:
            return 'yesterday';
        default:
            return abs($days).' days ago';
    }
}

This is what I don’t understand, my second function in action:

dateString2(new DateTime(date('l, j F', $timestamp)))

How do I add a parameter and use it in the context above?
Something like:

dateString(new DateTime(date('l, j F', $timestamp)) 'some-value-here')

Just can’t figure out how to do this, any help suggestions thanks?

Example, the two different parameter values are below:

default:
            return $dt->format('l, j F');
default:
            return abs($days).' days ago';

Barry

Arguments are separated by commas, and so to pass another argument to your dateString function, you’d invoke the following:


dateString(new DateTime(date('l, j F', $timestamp)), 'some-value-here');

Merging your two functions together, I believe the following should work:


function dateString(DateTime $dt, $switch = false) { 
    $dt->modify('midnight'); 
    $now = new DateTime('midnight'); 
    $diff = $now->diff($dt); 
    $days = $diff->days; 
    if ($now > $dt) { 
        $days *= -1; 
    } 
     
    switch (true) { 
        case $days === 0: 
            return 'Today'; 
        case $days === 1: 
            return 'Tomorrow'; 
        case $days === -1: 
            return 'Yesterday'; 
        case $days < -1: case $days > 1:
            if(!$switch && $days < -1)
                return abs($days).' days ago';
            if($switch && $days > 1)
            	return $days.' days';
        default:
            return ($switch) ? abs($days).' days ago' : $dt->format('l, j F'); 
    } 
}

You invoke dateString($dateTimeObject) to invoke your dateString function as normal, and to invoke your dateString2 function, you simply call dateString($dateTimeObject, true).

Good work!
Works first time :cool:

So basically now with the updated code I can either add:

dateString(new DateTime(date('l, j F', $timestamp)));

//  or

dateString(new DateTime(date('l, j F', $timestamp)), true);

// or use both

dateString(new DateTime(date('l, j F', $timestamp)))
. ' - ' . dateString(new DateTime(date('l, j F', $timestamp)), true);


Is that correct tpunt?

Yes, that’s correct.

Ok, say we want to add a second option into the mix when everything is true.
Do we simply add another if statement into that case?

Example from:

case $days < -1: case $days > 1:
            if(!$switch && $days < -1)
                return abs($days).' days ago';
            if($switch && $days > 1)
                return $days.' days';

We could add something similar into:

case $days === 0:
            return 'Today'; 

If true, return no value ’ ’
Meaning today will not show, if everything is true?

In theory, we could have if statements inside every case if we wanted to expand on this, is that right?
We could even add another argument?

Just getting an understanding here thanks.

This is what I mean, updated:

switch (true) {
        case $days === 0:
            if(!$switch)
                return 'Today';
                else {
                  if($switch)
                return '';
                }
        case $days === 1:
            return 'Tomorrow';
        case $days === -1:
            return 'Yesterday';
        case $days < -1:
            if(!$switch)
                return abs($days).' days ago';
        case $days > 1:
            if($switch)
                return $days.' days';
        default:
            return ($switch) ? abs($days).' days ago' : $dt->format('l, j F');
    } 

Barry

You can extend the code snippet like that, though your code will not function as expected. The problem is with the following two case statements:


case $days < -1:
    if(!$switch)
        return abs($days).' days ago';
case $days > 1:
    if($switch)
        return $days.' days';

The problem is with the cascading style of the case statements. If the condition $days < -1 is true, and if your $switch variable is true, then your first return statement will not execute. The PHP interpreter will, however, then continue to execute the body of the next case statement (regardless of the condition), and consequent case statement bodies until a statement is reached that breaks the execution cycle (like a return, break, continue, yield, die, exit, throw, etc, statement). So in fact, the above can be re-written as follows:


case $days < -1:
    if(!$switch)
        return abs($days).' days ago';
case $days < -1:
case $days > 1:
    if($switch)
        return $days.' days';

Or in the form of an IF statement:


if($days < -1) {
    return abs($days).' days ago';
}
if($days < -1 || $days > 1) {
    if($switch)
        return $days.' days';
}

The body of your first case statement, case $days === 0, could also be simplified:


switch (true) { 
    case $days === 0: 
        return ($switch) ? 'Today' : '';
    // other case statements
}

(Look in the ternary operator.)

If you intend on extending your switch statement much further though, then I’d probably use an IF/ELSIF/ELSE statement instead (because of the aforementioned complications of switch statements).

Getting slightly confusing now.

So in fact, the above can be re-written as follows:

Are you saying your initial code is incorrect?

I’m also now seeing on past events -34 days and not -34 days ago?
Is this something to do with the update?

Thanks, Barry

The code snippet in my first post should work fine - it’s your updated switch statement in your previous reply that is giving you those problems (for the aforementioned reasons).

Cool thanks tpunt.
Given me some good examples here should be able to fix this now :cool:

Thanks for your help!

Barry