Jump to content
imgrooot

Is there a way to remove the + sign from date diff? And is it possible to convert it to months?

Recommended Posts

So this is a simple code that finds out the difference between two dates and displays it in number of days.

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");

// RESULT
+272 days

 

My first question. Is it possible to remove the + sign in the result above?

Second question. Is it possible to show "months" if it's greater than 30 days? And years if the days are greater than 365?

How would I do this?

Share this post


Link to post
Share on other sites
Posted (edited)

Feeling magnanimous today, and having struggled to get to the correct source in the manual I"ll give you this one.

Drop The R.

As for showing it in months - the format options only show whole numbers so if you have a gap of less than a month you will show 0 when formatting it.  And if it is more than a month you will get a value of "months" but not the full value of months & days without doing some work on figuring out how many days were in the months that did transpire.

Edited by ginerjm

Share this post


Link to post
Share on other sites

As for showing it in months -  The manual does show you how but it is not immediately clear.

After dropping the R, change the a to d

Share this post


Link to post
Share on other sites
2 hours ago, ginerjm said:

As for showing it in months -  The manual does show you how but it is not immediately clear.

After dropping the R, change the a to d

Tried your way and it didn't work. 

Tried it this way and it works.

$diff->format("%a days");

 

Share this post


Link to post
Share on other sites

Yes - that gets you days without the + sign.   My last change was to show the months and leftover days, if you wanted it.

$dt1 = '2009-10-11';
$dt2 = '2009-10-13';
echo "Using dates of $dt1 & $dt2 :<br>";
$interval = GetInterval($dt1, $dt2);
$test = $interval->format('%m');
if ($test > 0)
	echo 'Interval is: '.$interval->format('%m months %d days');
else
	echo 'Interval is: '.$interval->format('%d days');
echo '<br><br>';
//   test 2
$dt1 = '2009-08-14';
$dt2 = '2009-10-13';
echo "Using dates of $dt1 & $dt2 :<br>";
$interval = GetInterval($dt1, $dt2);
$test = $interval->format('%m');
if ($test > 0)
	echo 'Interval is: '.$interval->format('%m months %d days');
else
	echo 'Interval is: '.$interval->format('%d days');
echo '<br><br>';
//   test 3
$dt1 = '2009-07-11';
$dt2 = '2009-09-25';
echo "Using dates of $dt1 & $dt2 :<br>";
$interval = GetInterval($dt1, $dt2);
$test = $interval->format('%m');
if ($test > 0)
	echo 'Interval is: '.$interval->format('%m months %d days');
else
	echo 'Interval is: '.$interval->format('%d days');
echo '<br><br>';
exit();
//******************************************
function GetInterval($dt1, $dt2)
{
	$datetime1 = date_create($dt1);
	$datetime2 = date_create($dt2);
	$interval = date_diff($datetime1, $datetime2);
	return $interval;
}

Here is some code for you to try out.

Share this post


Link to post
Share on other sites

Note that instead of using the format method to get the months value ...

$test = $interval->format('%m');

you can get it directly from the "m" property of the interval object...

$test = $interval->m;

Similarly, you can use the ->days property instead of ->format('%a')

Share this post


Link to post
Share on other sites

Good to know for those who regularly use the OO method of coding!

Share this post


Link to post
Share on other sites

Not sure what you are trying to say with that comment.

How would you use date intervals without "the OO method of coding"?

Share this post


Link to post
Share on other sites

I don't often code like your example, I use the functions/methods rather than properties.

Share this post


Link to post
Share on other sites

But

$test = $interval->format('%m');

was your example. Are you claiming that isn't OO?

Share this post


Link to post
Share on other sites

As I said - I prefer the methods, not the properties directly

Share this post


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.   Restore formatting

  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.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.