Jump to content

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


imgrooot

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?

Link to comment
Share on other sites

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
Link to comment
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");

 

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.