PHP Saved Program: calm-palace

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


<?php
$months = array('january'=>31, 'february'=>28, 'march'=>31, 'april'=>30,
    'may'=>31, 'june'=>30,'july'=>31, 'august'=>31, 'september'=>30, 'october'=>31,
    'november'=>30, 'december'=>31);
$flags = array('from', 'after', 'in', 'upcoming', 'next');
$days = array('sunday'=> 0, 'monday'=> 1, 'tuesday'=> 2,
    'wednesday'=> 3, 'thursday'=> 4, 'friday'=> 5, 'saturday'=> 6);
$units = array('days', 'day', 'weeks', 'week', 'months', 'month');

$tokens = array('from','blah', 'on','april','3000');
$tokens = condense_months($tokens);
print_r($tokens);


function condense_months($tokens) {
    global $months;
    $len = count($tokens);
    for ($x = 0; $x < $len; $x++) {
        $word = $tokens[$x];
        if (array_key_exists($word, $months)) {
            $check_aux = False;
            $d = 1;
            if (is_numeric($tokens[$x+1])) {
                $check_aux = True;
                $pd = (int) $tokens[$x+1];
                if (1 <= $pd and $pd <= $months[$word]) {
                    $d = (string)$pd;
                    echo "flag";
                }
            }
            echo $d;
            $time = strtotime($word.' '.$d);
            $date_str = date('Y-m-d 23:59:59', $time);
            if (strtotime($time) <= time()) {
                $new = date_create($date_str);
                $new -> modify('+1 year');
                $final = $new->format('Y-m-d 23:59:59');
            }
            else
                $final = $date_str;
            $tokens[$x] = $final;
            if ($check_aux)
                $tokens[$x+1] = $final;
        }
    }
    return $tokens;
}
?>
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs