Php calculete the cost for each day passed [duplicate] - php

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP calculate days Between 2 different dates [duplicate]
(2 answers)
Closed 9 years ago.
$1date =$row['Date1'];
$2date = $row['Date2'];
$datediff = $1date - $2date;
echo $datediff;
I want to count the days between and put into a table the result(10 dollars for each day passed)

// convert to unix timestamp
$1date = strtotime($row['Date1']);
$2date = strtotime($row['Date2']);
// 86400 seconds in a day
// floor to round down, change to ceil to round up
$datediff = floor(($1date - $2date) / 86400);
$cost = $days * 10;

You could do it in MySQL then work it into a variable. Do the initial call
SELECT TIMESTAMPDIFF('Date1','Date2');

Try:
$days = date_diff(date_create($row['Date1']), date_create($row['Date2']))->format('%a');
$cost = $days * 10;

Related

How to get 1 hours before [duplicate]

This question already has answers here:
Create Variable in PHP Equal to Current Time Minus One Hour
(8 answers)
Closed 3 years ago.
how to get 1 hours before in php
I try with:
but not working
$jam = "10:00:00";
$1hrsbefore = strtotime($jam), strtotime(-1 hours);
Try this code :
$jam = "10:00:00";
$timeadd= strtotime($jam) - 60*60;
$time = date('H:i:s', $timeadd);
echo $time;
Try this
$jam="10:00:00";
$time = date('H:i:s', strtotime($jam) - 60*60);
echo $time;

How to sum up time in php? [duplicate]

This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;

How to calculate interval in 30 minutes between two dates? [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have for example:
$first = '2014-05-18 12:00:00';
$last = '2014-05-18 13:30:00';
and i would like calculate interval in 30 minutes for these dates.
For this example interval = 3.
Other example:
$first = '2014-05-18 11:00:00';
$last = '2014-05-18 14:00:00';
interval = 6;
How is the best way for this in PHP?
Simple math solution
$s=floor(abs(strtotime($first)-strtotime($last))/30/60);
Here is fiddle
https://eval.in/153120
function timeDiff30minutes($first,$last){
$first = new Datetime($first);
$last = new Datetime($last);
$time_diff = $first->diff($last);
$time_diff_minute = $time_diff->days * 24 * 60;
$time_diff_minute += $time_diff->h * 60;
$time_diff_minute += $time_diff->i;
return $time_diff_minute/30;
}
timeDiff30minutes('2014-05-18 11:00:00','2014-05-18 14:00:00') // output 6

Difference of hours between two dates (integer) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate number of hours between 2 dates in PHP
How can I get the number of hours of difference between two dates in PHP? I need to get an integer since I want to know if is bigger or smaller than a particular value.
Try:
$date1 = "2012-11-05 12:35:00";
$date2 = "2012-11-07 14:35:00";
$diff = strtotime($date2) - strtotime($date1);
$diff_in_hrs = $diff/3600;
print_r($diff_in_hrs);
Manual
Demo
If you have an up-to-date PHP
$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');
// Procedurally
$interval = date_diff($dateOne, $dateTwo);
// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);
See: http://www.php.net/manual/en/class.dateinterval.php
<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>

Calculate date interval in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Subtracting dates in PHP
I have two Unix timestamps, how can I calculate the total number of days between them?
$timestamp1 = x;
$timestamp2 = y;
$days_elapsed = floor(($timestamp2 - $timestamp1)/86400);
echo $days_elapsed;
Convert them to UNIX-timestamp (if they arent already), then just
$diff = abs($timestamp1 - $timestamp2);
$days = (int) ($diff / 60 / 60 / 24);
something like this should do the trick:
$days = (strtotime($timestamp1)-strtotime($timestamp2))/(60*60*24);
This code should do the trick
$numDays = abs($timeOne - $timeTwo)/60/60/24;

Categories