Jump to content

Recommended Posts

I'm trying to subtract get the total number of days between two dates. I have been trying the DateTime date_diff method and the strtotime methods. I have it working mostly; but I have a problem with if the value should be negative. I've tried the invert() on the value that comes back from the date_diff() function but get errors. To sum up what I'm trying to do is:

if ( $diff === 0 ) 
  echo "Today"
elseif ($diff > 0)
  echo "+" . $diff . "Days";
else 
  echo "LATE -" . $diff . "Days";

Something like that. Here is my code...

		//retrieve expected complete time
		if ( isset( $stat[ 'expected_complete_time' ] ) ) {
			$today = new DateTime("now");
			$expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]);
			$today->setTime(12, 0, 0);
			$expected_complete_time->setTime(12, 0, 0);
			
			//calculate difference
			$eta = date_diff($expected_complete_time, $today)->format("%d");;
			
			//future
			if ($today > $expected_complete_time) {
				$eta = '+' . $eta;
			//today
			} elseif ($eta === '0') {
				$eta = 'Today';
			//overdue
			} else {
				$eta = 'LATE:-' . $eta;
			}

		} else 
			$eta = '';

 

Link to comment
https://forums.phpfreaks.com/topic/310671-difference-between-datetimes/
Share on other sites

No matter the method I use I get positive dates when they should be negative? Here's changes to my code and the results...

$today = new DateTime("now");
$expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]);
$eta = $expected_complete_time->diff($today);
			
$str_eta = $eta->format('%R%d');
echo ': Now: ' . $today->format('d/m/Y') . ': Expected: ' . $expected_complete_time->format('d/m/Y') . ' Diff: ' . $str_eta;

Now: 01/05/2020: Expected: 22/04/2020 Diff: +9 
Now: 01/05/2020: Expected: 01/05/2020 Diff: +0 
Now: 01/05/2020: Expected: 01/05/2020 Diff: +0 
Now: 01/05/2020: Expected: 22/04/2020 Diff: +9 
Now: 01/05/2020: Expected: 23/04/2020 Diff: +8 
Now: 01/05/2020: Expected: 04/05/2020 Diff: -2

8 minutes ago, mongoose00318 said:

Now: 01/05/2020: Expected: 22/04/2020 Diff: +9 
Now: 01/05/2020: Expected: 01/05/2020 Diff: +0 
Now: 01/05/2020: Expected: 01/05/2020 Diff: +0 
Now: 01/05/2020: Expected: 22/04/2020 Diff: +9 
Now: 01/05/2020: Expected: 23/04/2020 Diff: +8 
Now: 01/05/2020: Expected: 04/05/2020 Diff: -2

Looks right to me. What do you think those should be?

I was trying to get something like:

Now: 01/05/2020: Expected: 22/04/2020 Diff: -9 
Now: 01/05/2020: Expected: 01/05/2020 Diff: 0 
Now: 01/05/2020: Expected: 01/05/2020 Diff: 0 
Now: 01/05/2020: Expected: 22/04/2020 Diff: -9 
Now: 01/05/2020: Expected: 23/04/2020 Diff: -8 
Now: 01/05/2020: Expected: 04/05/2020 Diff: +2 (but shouldn't this actually be +3?)

Basically user says the amount of days it takes to complete some work and if today's date  is passed that day then it shows a negative value.

Edited by mongoose00318

Oh. Well that's easy:

You simply have the diff backwards. $object->diff($date) is the diff from $object to $date, and works like $date minus $object. $today->diff($tomorrow) is positive, $today->diff($yesterday) is negative.

Also, it must be rounding or something? If I set the date for tomorrow; the difference I get is 0. If I put the expected date 2 days out; I get back a difference of 1. Maybe it's using the time in combination with date and not getting a whole day for the difference because of that? The date in the database doesn't have a time. It's just the date.

Edited by mongoose00318

What do you think about this?

$today = new DateTime("now");
$expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]);
$eta = $today->diff($expected_complete_time);
			
$str_eta = $eta->format('%r%d');

if($expected_complete_time->format('d/m/Y') === $today->format('d/m/Y')) {
	$eta = 'Today';
} else {
	$eta = ($str_eta+1 == 1) ? $str_eta+1 . ' day' : $str_eta+1 . ' days';
}

https://imgur.com/HTiuvrF

Seems to be doing what I need.

57 minutes ago, mongoose00318 said:

Oops..yea that fixed it. But. I'm getting - and + on 0 values...? Can I just tell it not to do anything for a 0?

Write your own code to format the string according to the diff and invert.

57 minutes ago, mongoose00318 said:

Shouldn't that say 3 days?

Not if it there was a fractional part of a day involved. It's not 2 days, it's 2-point-something days.

56 minutes ago, mongoose00318 said:

The date in the database doesn't have a time. It's just the date.

PHP does not have a "just the date" type. There's always a time value...

34 minutes ago, mongoose00318 said:

What do you think about this?

It's still not quite right. Check the docs for DateInterval and format to make sure you're using them correctly.

1 hour ago, mongoose00318 said:

Also, on this date...

Now: 01/05/2020: Expected: 04/05/2020 Diff: +2 

Shouldn't that say 3 days?

Yes

$a = new DateTime('2020-05-01');
$b = new DateTime('2020-05-04');

echo $a->diff($b)->days;                //-> 3

 

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.