Site icon Shaharia's Blog

Find out How Many Times Remaining in PHP Programming

Suppose you have a date  2012-12-20 and today is 2012-12-18. What’s the different You can now see the ‘Time Remaining to 2012-12-18’ by implementing the following code.

/**
     * @param null $fromTime
     * @param null $toTime
     * @param bool $is_simple
     * @param array $options
     * @return string
     * @copyright     Shaharia Azam 2012-2020, Shaharia Azam Personal Project. (http://shahariaazam.com/)
     * @link          http://shahariaazam.com Personal Project
     * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
     */
    function TimeAfter($fromTime = null, $toTime = null,$is_simple=true, array $options=array('before'=>false,'after'=>false))
    {
        if(is_array($options)){
            if(array_key_exists('before',$options) && !empty($options['before']))
                $before=$options['before']; else $before="";
            if(array_key_exists('after',$options) && !empty($options['after']))
                $after=$options['after'];   else $after="";
        }
        if (!empty($fromTime) && !empty($toTime))
        {
            $start_date = new DateTime($toTime);
            $since_start = $start_date->diff(new DateTime($fromTime));

            if($is_simple === true)
            {
                if($since_start->m == "0")
                {
                    return $before.$since_start->d." days".$after;
                }
                return $before.$since_start->m." months ".$since_start->d." days".$after;
            }
            else
            {
                if($since_start->Y == "0")
                {
                    return $before.$since_start->m." months ".$since_start->d." days".$after;
                }
                return $before.$since_start->Y." years ".$since_start->m." months ".$since_start->d." days".$after;
            }

        }
    }

Now you can use TimeAfter() with all your required arguments.

Happy Coding!!

Exit mobile version