I have two intervals:
$intervalFrom = DateInterval('12:00')
$intervalTo = DateInterval('1:30')
Output must be:
DateInterval('10:30')
I try
$newInterval = $intervalFrom->sub($intervalTo);
But It not work
You could create 2 times a DateInterval using PT12H and PT1H30M.
To get a difference, you could use the DateTime diff method.
Create 2 datetimes and pass 2 fixed starting points like "midnight" and for both of them add an interval.
Retrieve the number of hours h and minutes i and use those to create a new DateInterval:
$intervalFrom = new DateInterval('PT12H');
$intervalTo = new DateInterval('PT1H30M');
$d1 = new DateTime("midnight");
$d1->add($intervalFrom);
$d2 = new DateTime("midnight");
$d2->add($intervalTo);
$diff = $d1->diff($d2);
$strResult = sprintf("PT%sH%sM", $diff->h, $diff->i);
$resultDateInterval = new DateInterval($strResult);
Demo
Related
I know how to calculate the number of days between two dates as shown below, but, how can I return the difference as a negative number if it's in the past?
This is what I'm using:
$today = new DateTime('now'); // today is 2015-09-02
$date1 = new DateTime("2015-08-13"); // 20 days ago
$diff = $today->diff($date1)->format("%a");
echo "diff is $diff"; // shows 20 but I want it to return -20
I've tried reversing the variables but it doesn't seem to care about the order.
DateInterval has the invert property that specifies whether they're positive or negative.
You can output it like
$interval->format("%r%a");
r stands for
Sign "-" when negative, empty when positive
See DateInterval::format docs.
$today = new DateTime('now'); // today is 2015-09-02
$date1 = new DateTime("2015-08-13"); // 20 days ago
$diff = $today->diff($date1)->format("%r%a");
Output will be -20. Also you can swap the dates likes following example. Output will be 20.
$today = new DateTime('now'); // today is 2015-09-02
$date1 = new DateTime("2015-08-13"); // 20 days ago
$diff = $date->diff($today)->format("%r%a");
this works fine in my project.
{{ \Carbon\Carbon::parse($firstdate)->diffInDays($seconddate,false) }}
output can be : -20
(Laravel)
I have on function which passing some parameter the like
everyWeekOn("Mon",11,19,00)
I want to compute the difference between the current day (e.g. 'Fri')
and passed parameter day i.e. Mon.
The output should be:
The difference between Mon and Fri is 3
I tried it like this
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But know I don't know what to do next to get the difference between the two days.
Note that this question is different from How to calculate the difference between two dates using PHP? because I only have a day and not a complete date.
Just implement DateTime class in conjunction with ->diff method:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
You can find no. of days in two days by using this code
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
Please use this may this help you
i have this dates
$dt_occ = mysql_result($info,0,"occ_data");
$dt_occ = strtotime($dt_occ);
$dt_occ = strtotime('+1 day' , $dt_occ);
$dt_unico = date('d/m/Y H:i',$dt_occ);
$dt_il = date('d/m/Y',$dt_occ);
$dt_alle = date('H:i',$dt_occ);
I need to know how many hours remain between now and $dt_unico
Take a look at the DateTime classes, they are much more flexible that strtotime() and date() (IMHO). Something like this will work for you:-
function getDiffInHours(\DateTime $earlierDate, \DateTime $laterDate)
{
$utc = new \DateTimeZone('UTC');
//Avoid side effects
$first = clone $earlierDate;
$second = clone $laterDate;
//First convert to UTC to avoid missing hours due to DST etc
$first->setTimezone($utc);
$second->setTimezone($utc);
$diff = $first->diff($second);
return 24 * $diff->days + $diff->h;
}
Use it like this for example:-
$hours = getDiffInHours(new \DateTime($dt_occ), (new \DateTime($dt_occ))->modify('+ 1 day'));
var_dump($hours); //24
I think this will work for you.
$dt1 = new DateTime($dt_occ);
$dt2 = new DateTime($dt_occ);
$dt2->modify("+1 day");
$interval = $dt2->diff($dt1);
echo $interval->hours;
If you're using PHP5.5 you can simply this a little bit:
$dt1 = new DateTimeImmutable($dt_occ);
$dt2 = $dt1->modify("+1 day");
$interval = $dt2->diff($dt1);
echo $interval->hours;
Since $dt_unico is derived from $dt_occ, which is a timestamp and time() gives the current time, also as a timestamp, subtracting the former from the latter will give the interval between them, in seconds.
Now, an hour is 60*60=3600 seconds, so:
$interval=(time()-$dt_occ)/3600;
Some notes, though:
I assumed that $dt_occ refers to the past. Future dates will give negative results, so if that's the case, switch the subtraction operands.
The above will give a floating point result. For an integral result, use the appropriate rounding function depending on the desired rounding method.
So in my database I got a datetime field, filled with e.g. 2012-09-19 11:20:33.
Now I'm trying to fetch the datetime.
$blabla = $something->getDatetime();
After that I create a new DateTime, which represents the time now
$now = new \DateTime("now");
And after that I want to subtract them like this (but it doesn't work)?
$test1 = strtotime($blabla);
$test2 = strtotime($now);
$diff = $test2 - $test1;
echo $diff;
My aim is to subtract the persisted datetime in the database from the time now...the result should be displayed in seconds...so 2012-09-19 11:22:22 - 2012-09-19 11:20:22 equals 120 (seconds).
I also tried to persist a unix timestamp into my database but unfortunately the field type timestamp doesn't exist.
If you want the answer displayed in seconds, then just subtract the timestamps:-
$blabla = $something->getDatetime();
$now = new DateTime();
$seconds = $now->getTimestamp() - $blabla->getTimestamp();
$blabla = $something->getDatetime();
$now = new \DateTime("now");
$diff = $now->diff($blabla);
Remember that $diff is a DateInterval object en you must use its methods and properties to get to the final desired result.
I have two timestamps recorded on a mysql table using php. How can I calculate the difference between these timestamps in hours using php or mysql?
If you actual hours (3600 seconds), it's just a matter of subtracting the timestamps and dividing by 3600:
$hours = ($timestamp2 - $timestamp1)/3600;
$hours = floor($hours); // to round down.
or similar in SQL.
If you want the "logical hours":
$tz = new DateTimezone("Europe/Lisbon"); //replace with actual timezone
$d2 = new DateTime("#$timestamp2");
$d2->setTimezone($tz);
$d1 = new DateTime("#$timestamp1");
$d1->setTimezone($tz);
$hours = $d2->diff($d1)->h;
This makes difference in case there has been a DST change between the two times. Example:
<?php
$ltz = new DateTimezone("Europe/Lisbon");
date_default_timezone_set("Europe/Lisbon");
$ts2 = strtotime("31-Oct-2010 02:30:00");
$ts1 = strtotime("31-Oct-2010 00:30:00");
$d2 = new DateTime("#$ts2");
$d2->setTimezone($ltz);
$d1 = new DateTime("#$ts1");
$d1->setTimezone($ltz);
var_dump(floor($ts2-$ts1)/3600);
var_dump($d2->diff($d1)->h);
gives:
float(3)
int(2)
in php you can just use regular math and then use the date() function to create the datetime represented in hours
Done in PHP, see this: http://php.about.com/od/advancedphp/qt/math_time_php.htm