PHP timestamp different in total remaining hour - php

i am implementing one application in which i have to display a time difference in total hour from end date to start date. suppose i have both start date and end date in timestamp.so how i will get the difference of date in total hour ?
now i am using the below code
$intervalo = date_diff(date_create(), date_create($end));
pr($intervalo);
and the output is like
DateInterval Object
(
[y] => 0
[m] => 1
[d] => 4
[h] => 18
[i] => 41
[s] => 2
[invert] => 0
[days] => 34
)
the above code show me the total hour baut the end date is greater then 1month it shows 1m in array . but i only want total number of hour only
can any one help me ?
thanks in advance

Don't use date_diff. Convert the dates to timestamps (which represent seconds), subtract the two, and divide by 3600.
$x = date_create();
$y = date_create($end)
$hours = ($y->getTimestamp() - $x->getTimestamp()) / 3600;
If you want hours, minutes, and seconds:
$x = date_create();
$y = date_create($end)
$diff = ($y->getTimestamp() - $x->getTimestamp());
$seconds = $diff % 60;
$diff = (int)($diff / 60);
$minutes = $diff % 60;
$diff = (int)($diff / 60);
$hours = $diff;

Related

PHP - Calculate Number of Months between a date and the current date

I need to work out the number of months between a date like this:
$inputDate = '09/08/2016';
that is entered in the MM/DD/YYYY format, and the current date, e.g.:
$today = date("m/d/Y");
I've been looking at the date_diff but can't seem to get the syntax right here and appreciate any help here.
You can use DateTime and diff
$datetime1 = new DateTime();
$datetime2 = new DateTime('09/08/2016');
$interval = $datetime1->diff($datetime2);
$months = $interval->format('%m months'); # you can also use %a days %h hours %i minutes %s seconds
echo $months;
# 8 months
# optimally you can use:
# echo $datetime1->diff($datetime2)->y*12;
# or
# echo $interval->m
Update based-on comments:
$datetime1 = new DateTime();
$datetime2 = new DateTime('09/08/2015');
$interval = $datetime1->diff($datetime2);
echo (($interval->format('%y') * 12) + $interval->format('%m')) . " full months difference";
Note:
A DateInterval Object looks like:
DateInterval Object
(
[y] => 3
[m] => 5
[d] => 15
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 1264
)
That's why we've to multiply the number of years * 12 and add the months in order to get the difference in months. Strange, but this is how it works...

What is the best way to subtract "after-hours" time from a date range to get an accurate "active" time?

I have 2 parallel arrays that contain mysql timestamps. The first array contains "Start" times, and the second array contains "Stop" times.
Example
Starts
Array ( [0] => 2014-12-05 12:21:29 [1] => 2014-12-10 07:14:17 [2] => 2014-12-10 12:43:47 [3] => 2014-12-12 07:39:28 [4] => 2014-12-12 08:13:30 )
Stops
Array ( [0] => 2014-12-08 08:08:37 [1] => 2014-12-10 10:13:37 [2] => 2014-12-12 07:18:53 [3] => 2014-12-12 08:10:39 [4] => 2014-12-12 08:27:26 )
I need to add the total times based off of all the starts and stops in each array, but I also need to subtract all the time that was "after-hours". This is for reporting purposes.
So let's say that any time after 4pm(16:00) and before 6am(06:00) needs to be removed from the total active time that will be reported.
I already have it subtracting weekend time, but I also need it to remove the "after-hours" time if it occurs after-hours on a weekday. The checks for weekday/weekend aren't a problem, but figuring out how to check if the time range contains hours that were after business hours is what is stumping me.
Here is how I am subtracting the weekend time:
function getWeekendSeconds($starts, $stops){
$count = 0;
$secondsToSubtract = 0;
$secondsInDay = 86400;
if(count($starts) > count($stops)){
while(count($starts) != count($stops)){
array_pop($starts);
}
}
foreach($starts as $start){
$stop = $stops[$count];
$startTime = strtotime($start);
$stopTime = strtotime($stop);
while($startTime < $stopTime){
$dayNum = date('N', strtotime(date("Y-m-d H:i:s", $startTime)));
if($dayNum == 6 or $dayNum == 7){
$secondsToSubtract += $secondsInDay;
}
$startTime = strtotime(date("Y-m-d H:i:s", $startTime) . " +1 day");
}
$count = $count + 1;
}
return $secondsToSubtract;
}
I was thinking of doing something similar to calculate the after-hours time, but I'm having trouble wrapping my head around it.
Would I just take the start time and add time to it and check it each time to see if it's greater than the business closing time? Is it simpler than I'm making it? Is there a mysql solution I'm not aware of?
Thanks in advance!

How to Minus two dates in php [duplicate]

This question already has answers here:
Date Difference in php on days? [duplicate]
(3 answers)
Closed 9 years ago.
i want to minus two dates in php
for example:
$date1 = 08/16/2013;
$date2 = 08/23/2013;
$answer = date2 - date1;
the $answer should be 7, How will i do that?
thank you so much
Start using DateTime class for date/time manipulation :
$date1 = new DateTime('08/16/2013');
$date2 = new DateTime('08/23/2013');
$diff = $date1->diff($date2);
print_r($diff); // or $diff->days
Output :
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 7
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 7
)
Read more for DateTime:diff().
Please note that various strtotime() examples are not correct in date/time difference calculation. The simplest example is difference between 2013-03-31 21:00 and 2013-03-30 21:00. Which for naked eye is exact 1 day difference, but if you do subtract this 2 dates, you will get 82800 seconds which is 0.95833333333333 days. This is because of the time change from winter to summer time. DateTime handles leap years and time-zones properly.
Try this -
<?php
$date1 = strtotime('08/16/2013');
$date2 = strtotime('08/23/2013');
echo $hourDiff=round(abs($date2 - $date1) / (60*60*24),0);
?>
You can get with strtotime and minus dates
$diff = abs(strtotime('08/16/2013') - strtotime('08/23/2013'));
echo $min = floor($diff / (60*60*24)); // 7
$date1 = '08/16/2013';
$date2 = '08/23/2013';
$days = (strtotime($date2) - strtotime($date1)) / (60 * 60 * 24);
print $days;

Calculating difference between dates

I need your help in how to subtract the last_modified and the final_manuscript_date in the following array:
Array (
[chapters_id] => 10736
[last_modified] => 2010-12-21 15:01:55
[steps_id] => 3
[sub_step_id] => 0
[steps_position] => 1
[final_manuscript_date] => 2010-09-27
)
So I can in this case get a value of N days between the dates 2010-12-21 and 2010-09-27?
Can't you simply do:
$diff = strtotime($arr["final_manuscript_date"]) - strtotime($arr["last_modified"]);
$days = $diff / 84600; // to get # of days, you can round them off or use ceil/floor
If you have 5.3+:
$date1 = new DateTime("2010-09-27");
$date2 = new DateTime("2010-12-21");
$interval = $date1->diff($date2);
echo $interval->d //returns days.
Have you checked strtotime?
http://php.net/manual/en/function.strtotime.php

Getting the time interval

I have two timestamp input one is the current time and another one is future
i.e.
Future time: 2010-8-17 23:00
Present time: 2010-8-15 11:00
I want to setup notification system which will display the time intervals between the above dates. i.e.
15 minutes before
30 minutes before
1 hour before
2 hour before
3 hour before
....
....
....
1 day before
I am not sure how to achieve this task in php, wondering if any one here can suggest me how to achieve this task
How about using the DateTime class.
<?php
$datetime1 = new DateTime('2010-8-15 11:00');
$datetime2 = new DateTime('2010-8-17 23:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days, %H hours');
?>
Output:
+2 days, 12 hours
$t1 = getdate($current_date);
$t2 = getdate($future_date);
return $t2[0]-$t1[0];
This will give you the difference between the two dates, measured in seconds.
Get the difference between the two dates and then use date_parse
// $difference = difference in times
print_r(date_parse($difference));
That will result in something along the lines of
Array (
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] => )
See http://php.net/manual/en/function.date-parse.php for more info
This is what I did when I needed the exact same thing.
You can easily extend it by getting the number of seconds in a week, month, or year.
function getTimeInterval($ts1, $ts2)
{
$interval = (int)$ts2 - (int)$ts1;
if ( $interval < 0 ) {
return false;
} else {
$days = floor($interval / 86400); // seconds in one day
$interval = $interval % 86400;
$hours = floor($interval / 3600);
$interval = $interval % 3600;
$minutes = floor($interval / 60);
$interval = $interval % 60;
$seconds = $interval;
}
return array(
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $seconds
);
}

Categories