开发者

PHP: replace date with string

开发者 https://www.devze.com 2023-03-28 08:29 出处:网络
I have a string containing multiple dates (from - to), separated by \"|\", example: 10.10.-29.10.2011 | 1.11.-31.11.2011

I have a string containing multiple dates (from - to), separated by "|", example:

10.10.-29.10.2011 | 1.11.-31.11.2011

I need a code that will compare the newest start ("fr开发者_如何学Goom") date (in this case 1.11.2011) and if this date is older than current date, then replace whole string with "CLOSED".

Anyone can help me with this?


Something like this?

$dates = "10.10.-29.10.2010 | 13.8.-31.11.2011 | 14.8.-31.12.2011";
$datesArr = explode(" | ",$dates);
foreach($datesArr as $k=>$date) {
    $split = explode(".-",$date);
    $from = explode(".",$split[0]); $to = explode(".",$split[1]);
    $from = $from[0].".".$from[1].".".$to[2];
    if(strtotime($from)<time()) unset($datesArr[$k]);
}
if(!empty($datesArr)) {
    if(count($datesArr)==1) $dates = $datesArr[0];
    else {
        $dates = implode(" | ",$datesArr);
    }
}
else $dates = "CLOSED";
echo $dates;
0

精彩评论

暂无评论...
验证码 换一张
取 消