Skip to content
PHP Logo

How can multiple date variables be combined with a specific separator in PHP?

To combine multiple date variables with a specific separator in PHP, you can use the date() function to format each date variable and then concatenate them with the desired separator. This allows you to create a single string that contains all the date values separated by a specific character.

php
$date1 = date('Y-m-d', strtotime('2022-01-01'));
$date2 = date('Y-m-d', strtotime('2022-02-01'));
$date3 = date('Y-m-d', strtotime('2022-03-01'));

$separator = '-';
$combinedDates = $date1 . $separator . $date2 . $separator . $date3;

echo $combinedDates; // Output: 2022-01-01-2022-02-01-2022-03-01