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
);
}
Related
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...
I want to find difference between these two date with days,hours and minutes.
$date1 = "27-09-2014 05:00 AM";
$date2 = "29-09-2014 03:00 PM";
From PHP Version > 5 below new date/time functions added to get difference:
$datetime1 = new DateTime("2010-06-20");
$datetime2 = new DateTime("2011-06-22");
$difference = $datetime1->diff($datetime2);
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
print_r($difference);
Result as below:
Difference: 1 years, 0 months, 2 days
DateInterval Object
(
[y] => 1
[m] => 0
[d] => 2
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 367
)
I'm not sure whether I get your question right, but shouldn't the following work?
$datetime1 = new DateTime('27-09-2014');
$datetime2 = new DateTime('29-09-2014');
$datetime1->setTime(05, 00);
$datetime2->setTime(15, 00);
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%a Day and %h hours'
DateTime::diff
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;
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;
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