This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Time diff in minutes between 2 dates
php date_diff in hours
i have:
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
this working ok, but how can i show only hours and minutes? For this example should be:
49 hours 10 minutes
From the PHP manual:
Note:
The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".
In other words, the DateInterval class won't do what you're asking for on its own; you'd have to do the calculation yourself.
Something like this should do it:
$hours = $interval->h + ($interval->d*24);
print "{$hours} hours ".$interval->format('%i minutes');
Related
I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.
This may be a complete noob question but here goes:
I have the following code that compares two dates for absence management. Where I expect the answer to return as 2 (the difference between start and end date) I get 1.
$start_time = new DateTime("2015-01-01 00:00:00");
$end_time = new DateTime("2015-01-02 00:00:00");
$diff = $end_time->diff($start_time);
$d = $diff->days; // 1
I have also tried using just the dates (but I need the times as some absence type are done by hours not days)
Difference is 1 because there is only one day difference between both days.
To convert the datetime into hours or minutes you should look to these links:
Convert datetime into year, month, days, hours, minutes, seconds
Difference between 2 time() values
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have 2 unix time, one is todays date and other is expiration time.
Todays Time: 1377173245 (2013-08-22 12:07:25)
Expiration Time: 1406303166 (2014-07-25 15:46:06)
What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?
Thank you :-)
This is what you're looking for: DateTime::diff
First create object with 1st date:
$date = new DateTime('2013-08-22 12:07:25');
Then use the diff method:
$diff = $date->diff(new DateTime('2014-07-25 15:46:06'));
Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.
Use DateTime::diff
See the doc on https://www.php.net/manual/en/datetime.diff.php
Try using datetime::diff. Here's the example from the php.net documentation:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.
Use DateTime class:
Example:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2014-07-25 15:46:06');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');
Outputs:
11 months, 21 hours, 44 minutes, and 20 seconds remaining
It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.
$days=($expiration-$today)/(60*60*24);
$hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder
$minutes=(($expiration-$today)/(60))%(60*60);
$seconds=($expiration-$today)/60;
If you need months, you'll find it easier to use date_diff().
i'm trying to use the class DateTime (php>=5.3) to calculate difference from 2 date.
The example from the manual is simple and clear, i tried that example and work good.
But if a change the start and end date, there a problem:
$this->start_date = '2011-03-01';
$this->end_date = '2011-03-31';
var_dump($this->start_date, $this->end_date);
$datetime1 = new DateTime($this->start_date);
$datetime2 = new DateTime($this->end_date);
$interval = $datetime2->diff($datetime1);
echo $interval->format('%a total days')."\n";
echo $interval->format('%m month, %d days');
Output is:
30 total days //ok
1 month, 2 days //no! i think it should be 0 month, 30 days
With march don't work very well! :)
Aren't there 28 days in February? It might be picking February for the "month" unit for some reason or other. The PHP documentation for the method seems to suggest this kind of thing could easily be the case. Saying "x Months" isn't overly useful anyway as a month isn't a fixed unit, it could be 28, 29, 30 or 31 days.
Extract from the PHP dateinterval format documentation below.
The DateInterval::format() method does
not recalculate carry over points in
time strings nor in date segments.
This is expected because it is not
possible to overflow values like "32
days" which could be interpreted as
anything from "1 month and 4 days" to
"1 month and 1 day".
i'm trying to use the class DateTime (php>=5.3) to calculate difference from 2 date.
The example from the manual is simple and clear, i tried that example and work good.
But if a change the start and end date, there a problem:
$this->start_date = '2011-03-01';
$this->end_date = '2011-03-31';
var_dump($this->start_date, $this->end_date);
$datetime1 = new DateTime($this->start_date);
$datetime2 = new DateTime($this->end_date);
$interval = $datetime2->diff($datetime1);
echo $interval->format('%a total days')."\n";
echo $interval->format('%m month, %d days');
Output is:
30 total days //ok
1 month, 2 days //no! i think it should be 0 month, 30 days
With march don't work very well! :)
Aren't there 28 days in February? It might be picking February for the "month" unit for some reason or other. The PHP documentation for the method seems to suggest this kind of thing could easily be the case. Saying "x Months" isn't overly useful anyway as a month isn't a fixed unit, it could be 28, 29, 30 or 31 days.
Extract from the PHP dateinterval format documentation below.
The DateInterval::format() method does
not recalculate carry over points in
time strings nor in date segments.
This is expected because it is not
possible to overflow values like "32
days" which could be interpreted as
anything from "1 month and 4 days" to
"1 month and 1 day".