Jump to content

Adding dates the easy way


Recommended Posts

I have a string with a date: $date="2011-08-23". All I want to do is add seven days to this to get "2011-08-30".There must be an easy way to do this with php. I'm fed up messing around with mktime and strtotime and exploding strings and all the rest. Please help otherwise it won't just be the string that explodes!

Link to post
Share on other sites
There must be an easy way to do this with php. I'm fed up messing around with mktime and strtotime and exploding strings and all the rest. Please help otherwise it won't just be the string that explodes!
If you already have a way to do it that works, why not just turn it into a function that you can include in your scripts when you need it? Then you don't need to rewrite everything when you need to modify a date.For example, this function:
function DateAdd($interval, $number, $date=null, $format=null) {	// Adds a specified inteval to a date/time	// If no $date is given, uses current date/time			// Check if a Date/Time was passed	if ($date) {		$date = strtotime($date);		if($date === -1) {			return("..date Invalid");		}	} else {		$date = strtotime("now");	}	$date_time_array = getdate($date);	$hours = $date_time_array['hours'];	$minutes = $date_time_array['minutes'];	$seconds = $date_time_array['seconds'];	$month = $date_time_array['mon'];	$day = $date_time_array['mday'];	$year = $date_time_array['year'];		switch ($interval) {	case 'yyyy':		$year+=$number;		break;	case 'q':		$year+=($number*3);		break;	case 'm':		//Test the new date to make sure only 1 month was added (Ex. 1 month to Jan 31 returns Feb 28 not Mar 3)		$tmpDate = mktime($hours, $minutes, $seconds, $month+$number, $day, $year);		if (date('d', $tmpDate) < $day) {			$tmpDiff = $day - date('d', $tmpDate);			$day = $tmpDiff;		}		$month+=$number;		break;	case 'y':	case 'd':	case 'w':		$day+=$number;		break;	case 'ww':		$day+=($number*7);		break;	case 'h':		$hours+=$number;		break;	case 'n':		$minutes+=$number;		break;	case 's':		$seconds+=$number;		break;            	}		$timestamp= mktime($hours, $minutes, $seconds, $month, $day, $year);		// format the $timestamp as specified or according to the locale settings	if ( $format ) {		return date($format, $timestamp);	} else {		return strftime('%c', $timestamp);	}}

...is one of many located in a separate file in our PHP includes directory here at work. Any time we need to work with dates, we include this file in our scripts so that all we have to do is call the function:$newDate = DateAdd('d', 7, $currDate, 'Y-m-d');

Link to post
Share on other sites

I know I can put it all into a function - ready to call up whenever its needed. But why can't it be simpler? Look at mysql. If I type this inSELECT DATE_ADD('2011-08-23', INTERVAL 7 DAY);it gives the answer. Surely php should be able to do the same just as easily? Working with dates must be a really common task when using php but it isn't really as easy as it should be.I think I am getting more impatient in my old age!

Link to post
Share on other sites
I know I can put it all into a function - ready to call up whenever its needed. But why can't it be simpler? Look at mysql. If I type this inSELECT DATE_ADD('2011-08-23', INTERVAL 7 DAY);it gives the answer. Surely php should be able to do the same just as easily? Working with dates must be a really common task when using php but it isn't really as easy as it should be.I think I am getting more impatient in my old age!
right, well the code you use in a function could just as easily not be in a function, but doing so just makes it simpler to transport that task across multiple applications. JSG's example is pretty concise; four lines.
Link to post
Share on other sites

SQL is actually pretty limited. It seems simpler in this case because one of its features does exactly what you want. PHP allows for a more precise handling of time. The price you pay for that is a few more steps.You just venting, or what?

Link to post
Share on other sites
You just venting, or what?
Yes, I suppose I am just venting but my point remains: working with dates in php is not as straightforward as it should be.justsomeguy's 4 lines of code does exactly what is required and is the way I've been doing it for ages but... its still 4 lines of code just to work out next week's date.
Link to post
Share on other sites

If your PHP > 5.2, this should work:

$d = "2011-08-23";$date = new DateTime($d);$date->modify('+7 day');echo $date->format('Y-m-d');

The first statement is data you already have. The last just verifies the concept. So really this is a 2-statement alternative.

Link to post
Share on other sites
  • 3 weeks later...

This topic is a little old, but I though OP might be interested in something I just learned.The strtotime function is capable of adding/subtracting intervals of time.

$tStamp = strtotime('5/25/2011');$tomorrow = strtotime('+1 day', $tStamp);$yesterday = strtotime('-1 day', $tStamp);$sevenDays = strtotime('+7 days', $tStamp);$oneWeek = strtotime('+1 week', $tStamp);

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...