0

We have code line:

 $result = "{$list[ 'ga_event_start_date' ]} - {$list[ 'ga_event_end_date' ]}";

And the dates display as 'd M y' We need to remove the 'y' numeral.

Can this be done in the original line above (and still be readable by the ave. coder)?

Currently we are doing with:

$newSDate = date("d M", strtotime($list[ 'ga_event_start_date' ])); 
$newEDate = date("d M", strtotime($list[  'ga_event_end_date'  ])); 
$result = "{$newSDate} - {$newEDate}";
1
  • Why not use native date types (\DateTimeInterface or Unix time) all the way through and have a custom format function for display purposes only? You would never manipulate numbers as random strings like Five, don't do it for dates either. Commented Feb 5 at 8:25

1 Answer 1

1

If you want to keep a one-line code, you can simply concatenate your current 3-line code and not use new variables.

$result = date("d M", strtotime($list['ga_event_start_date'])) . " - " . date("d M", strtotime($list['ga_event_end_date']));

If you want to make it readable, you can make a function for it, which also make your code reausable.

function formatDate($startDate, $endDate) {
    return date("d M", strtotime($startDate)) . " - " . date("d M", strtotime($endDate));
}

This is all they need to see.

$result = formatDate($list['ga_event_start_date'], $list['ga_event_end_date']);
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent - thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.