Re: Simple date question

From: Date: Wed, 30 Jul 2003 18:45:12 +0000
Subject: Re: Simple date question
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-157570@lists.php.net to get a copy of this message
"David Nicholson" <me@djnicholson.com> wrote in message news:ae490cce121bcb4989be859bcbda433a@home.djnicholson.com... Hello, This is a reply to an e-mail that you wrote on Wed, 30 Jul 2003 at 19:18, lines prefixed by '>' were originally written by you. > If I have: > $firstdate = "2003-06-28"; > then how can I get $firstdate plus 4 days? > Thanks! date("Y-d-m",mktime(0,0,0,substr($firstdate,5,2),substr($firstdate,8,2),subs tr($firstdate,0,4)) + (60*60*24*4)); Should do the trick, you can probably use strtotime instead of all the substr()'s but at a guess I would expect the above to be quicker (not that I have benchmarked it). HTH David. -------------------------------------------------- David's reply was spot on but I thought I'd break it down for you in case you didn't understand... $firstdate = "2003-06-28"; // Parse the date list($year, $month, $day) = explode("-", $firstdate); // Get the timestamp for this date.. $ts = mktime(0,0,0,$month,$day,$year); // Add four days to timestamp.. $ts += 345600; // Get year, month, day for new date.. $newdate = date("Y-m-d", $ts); The only thing that I'm doing differntly is the way in which I'm parsing the date. I'm using the list()=explode() method as opposed to substr() becuase this method is not prone to fail when values are not zero filled. Good luck, Kevin

« previous php.general (#157570) next »