TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   date() + 30 days = xxx (http://www.talkphp.com/general/3641-date-30-days-xxx.html)

paulOr 11-22-2008 11:05 PM

date() + 30 days = xxx
 
Ok so iv been trying to figure out how to do this little thing:

i have todays date: 22 November 2008 and i want to add 30 days onto it, and end up with another date, which would be 22 December 2008.

so like:
PHP Code:

date('j F Y') + 30 new_date('j F Y'

Is there a easy way of doing this? Im so stuck!

codefreek 11-22-2008 11:24 PM

PHP Code:

$d mktime(0,0,0,$month,$day,$year);
$end_date date(”Y m d”,strtotime(+2 days”,$d));

//To add days to current date
date(’Y-m-d’strtotime(+2 days”)); 


paulOr 11-22-2008 11:26 PM

well, the date is just coming from date() but new_date will be stored in mysql :P

Tanax 11-22-2008 11:29 PM

Quote:

Originally Posted by codefreek (Post 19845)
is it for mysql ? then i know :P

Doesn't matter? xD

Just use it like this:
php Code:
$endDate = date("D F d Y", strtotime("+30 days"));

paulOr 11-22-2008 11:31 PM

PHP Code:

date('j F Y'strtotime('+30 days')); 

got it :)

codefreek 11-22-2008 11:35 PM

i was thinking on something else sorry but i posted my way but tanax was faster oh well ;)

Wildhoney 11-23-2008 01:56 AM

I created this class to modify the dates using the strtotime function. Use it if you wish!

php Code:
class Date_Calculate
{
    private $m_iFromTimestamp;
   
    public function __call($szCall, $aArgs)
    {
        if (preg_match('~^to(Minus|Plus)(\d+)(.+)$~i', $szCall, $aMatches))
        {
            $szOperator = $aMatches[1] == 'Minus' ? '-' : '+';
            $iTimestamp = !empty($this->m_iFromTimestamp) ? $this->m_iFromTimestamp : time();
            return date($aArgs[0], strtotime($szOperator . $aMatches[2] . ' ' . $aMatches[3], $iTimestamp));
        }
       
        throw new Exception('Invalid date to syntax specified');
    }
   
    public function from($iTimestamp)
    {
        if (!is_integer($iTimestamp))
        {
            $iTimestamp = strtotime($iTimestamp);
        }
       
        $this->m_iFromTimestamp = $iTimestamp;
        return $this;
    }
}

$pDate = new Date_Calculate();

echo $pDate->from(time())->toMinus4Days('jS M Y');
echo $pDate->toPlus5Years('jS M Y');

codefreek 11-23-2008 07:27 AM

looks great, Wildhoney :D
thanks for sharing will probably learn something from it will,
look at it some more later..

Kalle 11-23-2008 09:25 AM

Damns some sweet logic Adam :)


All times are GMT. The time now is 03:02 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0