I have two dates that are in format Y-m-d
$dateOld = new DateTime("2017-01-10");
$dateNew = new DateTime("2017-01-11");
echo $diff = $dateNew->diff($dateOld)->format("%a");
this is working perfect and giving me exact days left.
But now I have added time and it is in H-M format
Like 23:38 and 17:21 and cannot understand now to get the difference between two dateTime
$dateOld = new DateTime("2017-01-10 23:38");
$dateNew = new DateTime("2017-01-11 17:21");
echo $diff = $dateNew->diff($dateOld)->format("%a");
I want to get the difference even if the value if in floating point. Now to work with date concatenated with time?
Use this:
<?php
$dateOld = new DateTime("2017-01-10 23:38");
$dateNew = new DateTime("2017-01-11 17:21");
$diff = $dateNew->diff($dateOld);
$days = $diff->d;
$hours = $diff->h;
$minutes = $diff->i;
$total_difference = $days + ($hours * 60 + $minutes) / 1440;
echo $total_difference;
Or, without the DateInterval:
$dateOld = new DateTime("2017-01-10 23:38");
$dateNew = new DateTime("2017-01-12 17:21");
$difference_in_seconds = $dateNew->getTimestamp() - $dateOld->getTimestamp();
$total_difference_in_days = $difference_in_seconds / 86400;
echo $total_difference_in_days;
Using ->format("%a") will give you the rounded days.
See http://php.net/manual/en/datetime.diff.php.
$dateNew = '2017-01-11 17:21';
$dateOld = '2017-01-10 23:38';
$dateNew = new DateTime($dateNew);
$dateOld = new DateTime($dateOld);
$diff = $dateNew->diff($dateOld);
echo $diff->format("%H:%I");
Source: http://php.net/manual/en/datetime.diff.php
Related
My code:
$one = new DateTime("2018-03-15 11:53:13");
$two = new DateTime("2018-03-15 13:53:00");
$diff = $one->diff($two);
$three = new DateTime("2018-03-15 11:52:55");
$four = new DateTime("2018-03-16 11:52:57");
$difftwo = $three->diff($four);
$day = $diff->format('%H:%I:%S');
$day2 = $difftwo->format('%H:%I:%S');
$secs = strtotime($day2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($day) + $secs);
echo $result;
- $day = 01:59:47
- $day2 = 00:00:02 and 1 day
Result : 01:59:49
But I want to show : 1 01:59:49 (1 is a day result of $day2)
Can someone help me find the solution?
You could create 2 new same date. In one of them, add your 2 intervals.
Then you could use the DateInterval object to get your value:
$one = new DateTime('2018-03-15 11:53:13');
$two = new DateTime('2018-03-15 13:53:00');
$diff = $one->diff($two);
$three = new DateTime('2018-03-15 11:52:55');
$four = new DateTime('2018-03-16 11:52:57');
$difftwo = $three->diff($four);
$d1 = new DateTime(); // Now
$d2 = new DateTime(); // Now
$d1->add($diff); // Add 1st interval
$d1->add($difftwo); // Add 2nd interval
// diff between d2 and d1 gives total interval
echo $d2->diff($d1)->format('%d %H:%I:%S') ;
Outputs :
1 01:59:49
I want to calculate how many weeks there are left from a specific date to another date, in order to get the budget per week. Here's my code:
$date_from = new DateTime('2015-07-28');
$date_to = new DateTime();
$interval = $date_from->diff($date_to);
$daysleft = ($interval->format('%a') + 1);
$weeksleft = number_format($daysleft / 7);
echo ('3164.49' / $weeksleft);
That code prints 3 167,76 for the last 2 weeks which of course is wrong. But what is wrong with my code?
$date_from = new DateTime('2015-07-28');
$date_to = new DateTime();
$interval = $date_from->diff($date_to);
$daysleft = ($interval->format('%a') + 1);
$weeksleft = number_format($daysleft / 7);
echo (floatval('3164.49') / $weeksleft);
Results
1582.245
You can do it in different way as shown below.
$a = strtotime('2015/07/28');
$b = time();
$diff = abs($a - $b);
echo round($diff/(60*60*24*7)); // to get round figure
try this function,
function datediffInWeeks($date1, $date2)
{
if($date1 > $date2) return datediffInWeeks($date2, $date1);
$first = DateTime::createFromFormat('m/d/Y', $date1);
$second = DateTime::createFromFormat('m/d/Y', $date2);
return floor($first->diff($second)->days/7);
}
var_dump(datediffInWeeks('1/2/2013', '6/4/2013'));// 21
I get 1582.245 .. Check/set your timezone settings.
date_default_timezone_set ($timezone_identifier)
http://php.net/manual/en/timezones.php
I been trying to fix this php percentage calculator of the day...basically right now here is about 230pm and I am getting 73% of the day completed....it should be more like 60% of the day. Here is the code:
$now = time();
$today = strtotime(date("m/d/Y"));
$seconds = $now - $today;
$day = 24*60*60;
$percent = $seconds / $day*100;
I attempted to write my own version but I am getting 100% of the day...Here is the code:
$todaysTime = time();
$todaysStart = time()-86400;
$todayCalc = $todaysTime - $todaysStart;
$dayPhpOne = 24*60*60;
$percentDay = $todayCalc / $dayPhpOne*100;
It is done in php where am I messing up my code?
Try this:
$percentDay = time() % 86400 / 864;
Edit
From the comments I take, that I didn't elaborate on time zones. Let me make clear, that this is meant to be UTC day percent.
This solution does respect timezone and other time-related complexities:
$d = new DateTime();
$h = $d->format('H');
$m = $d->format('i');
$s = $d->format('s');
$currentSecond = $h * 3600 + $m * 60 + $s;
$midnight = new DateTime($d->format('Y-m-d'), $d->getTimezone());
$tomorrow = clone $midnight;
$tomorrow = $tomorrow->add(new DateInterval('P1D'));
$secondsToday = $tomorrow->getTimestamp() - $midnight->getTimestamp();
$percent = $currentSecond / $secondsToday * 100;
var_dump($percent);
If necessary - it's possible to specify a particular timezone to be used as a second DateTime constructor argument.
How do I get the difference between two dates based on a 360 days?
360-days: http://en.wikipedia.org/wiki/360-day_calendar
I want to get the difference in days, years and months.
for example:
$fechaDT1 = new DateTime($fechauno);
$fechaDT2 = new DateTime($fechados);
//$initialdays = 30 - (float)$fechaDT1->format('d');
$years = $fechaDT1->format('Y') - $fechaDT2->format('Y');
$months = $fechaDT1->format('m') - $fechaDT2->format('m');
$days = (-$fechaDT1->format('d') + $fechaDT2->format('d'));
$totalDay = $months*30 +$days;
solution:
$startDate = new DateTime($startDate);
$endDate = new DateTime($endDate);
$initialDays = 30 - $startDate->format('d');
$year = ($endDate->format('Y') - $startDate->format('Y')) * 360;
$meses = ($endDate->format('m') - $startDate->format('m')) * 30;
$dias = ($endDate->format('d') - $startDate->format('d'));
$totalDays = $year+$meses+$dias;
$years = number_format($totalDias/360);
$diff = $years - ($endDate->diff($startDate)->y);
$daysR = $totalDays - (($years-$diff)*360);
$result = array("days" => $daysR, "years" => ($years-$diff), "initial days" => $initialDays);
return $result;
The very best solution:
<?php
$date1 = new DateTime('2013-03-24');
$date2 = new DateTime('2014-03-24');
$diff = $date1->diff($date2);
// Do whatever you want
echo $diff->days;
var_dump($diff);
Reference
Nettuts article
There are many other functional options as well, but today, OOP way is better.
Update: 360 days thing
Years:
$years = ($diff->days - ($diff->days % 360)) / 360; //+some remaining days if any
Month: According to the wiki page and following The US/NASD Method (30US/360):
$months = ($diff->days - ($diff->days % 30)) / 30; //+some remaining days if any
I am new to Python also but I think this will work:
import datetime as dt
import calendar
Calculate days difference based on 30/360 calendar
formats
date1 : date format (2020-01-01)
date2 : date format (2020-01-01)
def days_360 (date1, date2):
days_diff = (date2.year - date1.year) * 360;
days_diff += (date2.month - date1.month) * 30;
days_diff += (date2.day - date1.day);
return days_diff;
I have a function that creates time intervals between two time marks. The function works but I'm struggling to upgrade from strtotime() and use the DateTime class.
Below is a patch of code I wrote without getting errors
$timestart = new DateTime("14:00:00");
$timestop = new DateTime("20:00:00");
$date_diff = $timestop->diff($timestart);
$time_diff = $date_diff->format('%H');
Next is the entire code untouched. I get DateInterval could not be converted to int erros using the code above. Please kindly advise how to correctly implement the class.
Live example: http://codepad.org/jSFUxAnp
function timemarks()
{
//times are actually retrieved from db
$timestart = strtotime("14:00:00");
$timestop = strtotime("20:00:00");
$time_diff = $timestop - $timestart; //time difference
//if time difference equals negative value, it means that $timestop ends second day
if ($time_diff <= 0)
{
$timestop = strtotime( '+1 day' , strtotime( $row->timestop ) ); //add 1 day and change the timetsamp
$time_diff = $timestop - $timestart;
}
//create interval
$split = 3;
$interval = $time_diff/$split;
//get all timemarks
$half_interval = $interval / 2;
$mid = $timestart + $half_interval;
for ( $i = 1; $i < $split; $i ++) {
//round intervals
$round_mid = round($mid / (15 * 60)) * (15 * 60);
$result .= date('H:i:s', $round_mid) . ", ";
$mid += $interval;
}
$round_mid = round($mid / (15 * 60)) * (15 * 60);
$result .= date('H:i:s', $round_mid);
return $result;
}
outputs 15:00:00, 17:00:00, 19:00:00
Actually you're using DateTime, these are just aliases for creating DateTime instances
The equivalent would look like this:
$timestart = new DateTime("14:00:00");
$timestop = new DateTime("20:00:00");
$date_diff = $timestop->diff($timestart);
$time_diff = $date_diff->format('%H');
So this has to work, I tested it and I got correct results!