0

I'm calculating the difference between 2 dates using DateTime() and it's working fine. The problem is that I want to have the days format be able to go above a full month so 30/31 or higher.

$now = new DateTime();
$future_date = new DateTime($contest->expires_at);

$interval = $future_date->diff($now);
$enddate = $interval->format("%m month, %d days, %h hours, %i minutes");

The current problem with this is that when I don't display the months, the days can only go up to 30/31 and anything over that will be carried over to make a new month resetting the days count with the leftover days. I want to be able to display 42 days when the difference is 6 weeks with this kind of format:

$enddate = $interval->format("%d days, %h hours, %i minutes");

Is there a quick fix for this or do I need to manually convert the timestamp to seconds and use my own function with modulus operators for this?

HamZa
  • 14,671
  • 11
  • 54
  • 75
Stephan-v
  • 19,255
  • 31
  • 115
  • 201

3 Answers3

1

You can try:

$enddate = $interval->format("%a days, %h hours, %i minutes");

See the DateInterval::format in the manual.

NOTE

Take care of the bug if you're working on windows.

Young
  • 7,986
  • 7
  • 43
  • 64
  • `$enddate = $interval->format("%m months, %d days, %h hours, %i minutes");` This gives me a difference of 0 months, 6 days, 23 hours, 52 minutes. Wich is correct since I'm counting down from a week. When I try: `$enddate = $interval->format("%a days, %h hours, %i minutes");` I'm getting 6015 days, 23 hours, 50 minutes? – Stephan-v Apr 25 '13 at 09:41
  • @user2294225 that's strange.I've test your code with "2013-06-01 00:00:00" as your `$contest->expires_at` and it shows me the correct response `36 days, 6 hours, 14 minutes`.What's your php version? – Young Apr 25 '13 at 09:46
  • @user2294225, it seems you have already found the bug on windows.:) – Young Apr 25 '13 at 09:55
1

This should solve your porblem:

$now = new DateTime();
$future_date = new DateTime();

// a period of 2 months
$addPeriod = new DateInterval('P2M');

// adding the period
$future_date->add($addPeriod);

// get the differnce
$interval = $future_date->diff($now);

echo($interval->days) . ' days';

For today: echo returns '61 days'

// EDIT

To avoid running into the dataInterval-Bug you can use:

$now = new DateTime();
$future_date = new DateTime();

// a period of 2 months
$addPeriod = new DateInterval('P2M');

// adding the period
$future_date->add($addPeriod);

// get the difference in second
$diffTimestamp = $future_date->getTimestamp() - $now->getTimestamp();

// convert to days
// 1 day = 86.400 seconds
$diffDays = $diffTimestamp/86400;

echo(floor($diffDays)) . ' days';
alsleben
  • 11
  • 3
  • I'm only getting 6015 for days. After looking it up I've found its a bug in this php version? [link to the stackoverflow post discussing the same problem](http://stackoverflow.com/questions/2519261/how-to-get-aggregate-days-from-phps-datetimediff) – Stephan-v Apr 25 '13 at 09:47
0

Update my php version since this is a bug in my old and it works perfectly now.

How to get aggregate days from PHP's DateTime::diff?

Community
  • 1
  • 1
Stephan-v
  • 19,255
  • 31
  • 115
  • 201