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