I have a date string formatted as March 8 - 10
where the year is not specified, but depending on the current day of the calendar year, it will be the dates in March of the next year.
What is the best approach to ensure an accurate year when a date similar to the one above passed on December 31st?
Thinking of something like below, using $sdate > $now
, however, it will add +1 year to any date greater than now, and will not consider December 31 as the end of this year.
$dates = trim('March 8 - 10');
$now = date("Y-m-d",strtotime("now"));
if (strpos($dates,'-') !== false) {
$sdate = trim(substr($dates, 0, strpos($dates, '-')));
if ($sdate > $now) {
$sdate = strtotime("+1 year", strtotime($sdate));
$sdate = date("Y-m-d", $sdate);
}
$month = substr($sdate, 0, strpos($sdate, ' '));
$edate = $month.substr($dates, -2, strpos($dates, '-'));
$edate = date("Y-m-d",strtotime($edate));
}
source
share