This question already has answers here:
PHP - time minus time to minutes
(2 answers)
php - One Hour Earlier Than Given Datetime
(5 answers)
Closed 5 years ago.
I have a string time $a = "17:20:00";
I want to do
if($a > "13:00:00"){
$c = $a - "01:00:00";
}
I want to get $c = "16:20:00"
How can this be done?
Thank you!
$a is a string and can't be compared like that.
Use strtotime to make it UNIX time (integer) and subtract 3600 seconds.
And then use date to convert back to string.
$a = strtotime("17:20:00");
if($a > strtotime("13:00:00")){
$c = $a - 3600;
}
Echo date("H:i", $c);
https://3v4l.org/fOggP
You should try this:
if(strtotime($a) > strtotime("13:00:00")){
$c = date( "h:i:s", strtotime($a) - strtotime("01:00:00"));
}
You should try this!!!
Related
This question already has answers here:
How to get millisecond between two dateTime obj?
(5 answers)
Closed 5 years ago.
For example:
$date1='2017-12-13 13:06:21.602';
$date2='2017-12-13 13:06:18.102';
$diff=$date1-$date2
echo $diff;
output should be 3.5
Use The DateInterval class
Note: This solution works since PHP 7.1.0 when was added parameter f.
// your code goes here
$date1 = new DateTime('2017-12-13 13:06:21.602');
$date2 = new DateTime('2017-12-13 13:06:18.102');
$interval = $date1->diff($date2);
echo $interval->s+$interval->f;
// s = seconds, f = microseconds, as a fraction of a second
//var_dump($interval);
The result is 3.5
https://ideone.com/kf1PQQ
For PHP version comparison since 4.3.0: https://3v4l.org/VCI6P
Here is a solution 5.2.2 < php version < 7.1
<?php
$date1=\DateTime::createFromFormat('Y-m-d H:i:s.u','2017-12-13 13:06:21.602');
$date2=\DateTime::createFromFormat('Y-m-d H:i:s.u','2017-12-13 13:06:18.102');
// $di = $date1->diff($date2);
// var_dump( $di );
// seconds
$diffSeconds = $date1->getTimestamp() - $date2->getTimestamp();
// milli
$diffMilli = ($date1->format('u') - $date2->format('u')) / 1000000;
//result :
$diffWithMilli = $diffSeconds + $diffMilli;
var_dump($diffWithMilli);
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;
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Closed 8 years ago.
I get the string data from the database
The data format like:
07:00:00.0000000
It means 7:00 am.
How can i convert this string into Date type for comparison.
Therefore, I can get 2 data. Such as
Start:
07:00:00.0000000
End:
16:30:00.0000000
After the time comparison, i can got a answer like 9.3.
You can use strtotime :
$start = strtotime("07:00:00.0000000");
$end = strtotime("16:30:00.0000000");
$difference = $end - $start;
Or with DataTime object :
$start = new DateTime("07:00:00.0000000");
$end = new DateTime("16:30:00.0000000");
$interval = $start->diff($end);
$difference = $end->getTimestamp() - $start->getTimestamp();
Then echo the result :
echo $difference; // in seconds
echo $difference / 60; // in minutes
echo $difference / 3600; // in hours
echo $interval->format('%s') // in seconds
It's then up to your preference. I also suggest you to have a look at this post regarding the performances of the two solutions.
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;
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
?>