Sum two different DateTime in PHP - php

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

Related

How to not calculate the weekend in penalty using php [duplicate]

This question already has answers here:
How to put a borrowdate into my calculation fines
(1 answer)
PHP day of week numeric to day of week text
(8 answers)
Closed 5 years ago.
I want to prevent calculation in fines when the day is Saturday and Sunday or Only Sunday not include in fines. I think it's better if we use the borrowdate,currentdate, and returndate to fully functions from my database Thank you!
$borrowdate = new Datetime($row['date_return']);
$returndate = new Datetime($row['due_date']);
$currentdate = new Datetime();
$fines = 0;
if($currentdate > $returndate){
$days = $borrowdate->diff($returndate ?? $currentdate, true)->days;
echo "₱ ". $fines = $days > 0 ? intval(floor($days)) * 15 : 0;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
}
You should count the number of working days, and than calculate your fine.
$returnDate= new DateTime( '2017-08-17' );
$today= new DateTime( '2017-08-21 00:23:44' );
$returnDate->setTime(0,0);
$today->setTime(0,0);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($returnDate, $interval, $today);
$weekendDays = 0;
$totalDays = 0;
foreach ( $period as $p ) {
$totalDays++;
if($p->format( "w" )==0 or $p->format( "w" )==6) $weekendDays++; //6 - saturday, 0 - sunday
}
echo "<p>Total days: $totalDays</p><p>Weekend days: $weekendDays </p>";
$fines = ($totalDays - $weekendDays) * 15;
echo "₱ ". $fines;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
do like this first find out the dayofweek of current date and check if its sunday or saturday. The value returned form day of week will be in number. 0 means Sunday and through 6 for saturday.
$borrowdate = new Datetime($row['date_return']);
$returndate = new Datetime($row['due_date']);
$currentdate = new Datetime();
$dayofweek = date('w', $currentdate->getTimestamp());
$fines = 0;
if($currentdate > $returndate){
if($dayofweek != 0){
$days = $borrowdate->diff($returndate ?? $currentdate, true)->days;
echo "₱ ". $fines = $days > 0 ? intval(floor($days)) * 15 : 0;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
}
}
$newquery = mysqli_query($dbcon,"select fines from borrowdetails where borrow_details_id = '$fi'");
$row = mysqli_fetch_assoc($newquery);
echo $row['fines'];

how to get the difference between two dates with timing

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

cant insert the difference of this into table

this is my insert section the $total_minutes and $total_hour cant be inserted in the table
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$hour = $_POST['hour'];
$min = $_POST['min'];
$sec = $_POST['sec'];
$year1 = $_POST['year1'];
$month1 = $_POST['month1'];
$day1 = $_POST['day1'];
$hour1 = $_POST['hour1'];
$min1 = $_POST['min1'];
$sec1 = $_POST['sec1'];
$time_in = $year.'-'.$month.'-'.$day.' '.$hour.'-'.$min.'-'.$sec;
$time_out = $year1.'-'.$month1.'-'.$day1.' '.$hour1.'-'.$min1.'-'.$sec1;
$total_minutes = $total_min;
$total_hour = $total_hr;
$sql = "INSERT INTO time (year, month, day, hour, min, sec, year1, month1, day1, hour1, min1, sec1, time_in, time_out, total_minutes, total_hour)
VALUES
('$year','$month','$day','$hour','$min','$sec','$year1','$month1','$day1','$hour1','$min1','$sec1','$time_in','$time_out', '$total_minutes', '$total_hour')";
how can i add this in my table?
minutes
$datetime1 = strtotime($row['time_in']); //year-month-day hr:min:sec timein
$datetime2 = strtotime($row['time_out']); //year-month-day hr:min:sec timeout
$interval = abs($datetime2 - $datetime1);
$total_min = round($interval / 60);
$total_minutes = $total_min;
hour
function convertToHoursMins($total_minutes, $format = '%02d:%02d') {
if ($total_minutes < 1) {
return;
}
$hours = floor($total_minutes / 60);
$wmin = ($total_minutes % 60);
return sprintf($format, $hours, $wmin);
}
$total_hr = convertToHoursMins($total_minutes, '%02d hours %02d minutes');
$total_hour = $total_hr;
im new and i wanted a simple answer
Use MySQL TIMEDIFF() function to get the answer. Instead of PHP, MySQL Function gives the result in very easiest manner.
$datetime1 = strtotime($row['time_in']); //year-month-day hr:min:sec timein
$datetime2 = strtotime($row['time_out']); //year-month-day hr:min:sec timeout
$timeInterval = mysql_query("SELECT TIMEDIFF($datetime2, $datetime1)");
$timeArray = explode(":", $timeInterval);
Hope this may help you :)
You have to convert your datetime1 and datetime2 in this format first :
datetime1 = 2012-01-01 12:00:00;
datetime2 = 2012-01-02 13:00:00;
Then in my sql insert query directly insert value from following functions :
TIMESTAMPDIFF(HOUR, datetime2, datetime1) // For Hours Calculation
TIMESTAMPDIFF(MINUTE,datetime2, datetime1) // For Minutes Calculation
Hope this may help you :)

Calculate budget per week and how many weeks there are left

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

how get calc saturday+monday in range date

i hope some help to me ...
i am trouble with some code IN PHP ,maybe you can see function about this :
I need to check is there on Saturday in one period ($date_start ,$date_end )
so :
input : $date_Start (ex: "2010-07-01")
$date_end (ex: "2010-07-12")
output : total saturday : 2 in your range date
but the way thanks before
JOKONARDI
Efficient implementation with no loops for number of Saturdays in the range:
$d1 = new DateTime("2010-07-01"); //including
$d2 = new DateTime("2010-07-12"); //excluding
$diff = $d2->diff($d1);
$days = $diff->format("%a");
$num = floor($days / 7);
$remaining = $days % 7;
$d = $d1->format("w");
if ($d + $remaining > 6)
$num++;
//result in $num

Categories