Add string variable (minutes) to time variable in php [duplicate] - php

This question already has answers here:
PHP Adding 15 minutes to Time value
(9 answers)
Closed 3 years ago.
I got some problems handling string and time.
I am reading a form which gives me a string like this: "08:00"
Now i am running a foreach loop after which i want to add e.g. 15 minutes to the upper string.
I tried to convert the "08:00" to a time with
$string = "08:00";
$time = date("H:i", strtotime($string));
echo $time; //echos 1577260800
How can i add e.g. 15 minutes or even better a string like $add = "10" to the $time? The following doesnt work.
$add = "10";
$newtime = $time + strtotime($add);

Just add time in seconds to an existing time.
$string = "08:00";
$timeInSeconds = strtotime($string) + 15*60; // 15*60 => 15 minutes in seconds
$time = date("H:i", $timeInSeconds );
echo $time; // shows 8:15

You can just use the strtotime's parsing of words.
Meaning you can just as you ask "add 10 minutes".
$string = "08:00";
$time = date("H:i", strtotime($string . " +10 minutes"));
echo $time; //8:10

Related

How to compare two time in PHP accurately? [duplicate]

This question already has answers here:
AM/PM comparison in PHP [duplicate]
(3 answers)
Closed 4 months ago.
time() function in PHP used to get current time.But there's a problem!
Suppose we get the current time 12:05 AM (EST) using this function, then if i compare this time with another time(For example,10:50 AM),obviously PHP will consider that the timestamp of 10:50 AM is greater than 12:05 AM)..but actually it's wrong!12:05 is basically 00:05 AM...!!!
I don't know how to compare times like this accurately,any help would be appreciated.
I'm using this block of code to compare those two times...
$current_time = time(); //suppose current time is 12:15 AM(returned by time() func)
$time1 = '1:30 AM';
$draw_time = strtotime($time1);
if($current_time < $draw_time1) {
echo 'Perfectos!';
}
$current_time = time();
$time1 = date('d.m.Y') . ' 11:42 AM';
$draw_time = strtotime($time1);
echo ($draw_time) . " " . $current_time . "\r\n";
if ($current_time < $draw_time) {
echo 'True';
} else {
echo 'False';
}
You can try this way
Your code is incorrect , bacause you get first variable for only hour and second variable for whole time

PHP Get last increment of five minutes [duplicate]

This question already has answers here:
Subtract time in PHP
(3 answers)
Closed 3 years ago.
I am looking to get the previous increment of five minutes from the current time...
Lets say that the current time is 12:07pm UTC
I want to put into a variable 12:05pm UTC
What would be an easy way of going about this?
You can do this with the DateTime class (https://3v4l.org/7aqMH):
<?php
$date = new DateTime('12:07 UTC');
$minutes = (int) $date->format('i');
$rounded = round($minutes / 5) * 5;
$date->setTime($date->format('H'), $rounded, $date->format('s'));
or more concisely:
<?php
$date = new DateTime('12:07 UTC');
$date->setTime(
$date->format('H'),
round($date->format('i') / 5) * 5,
$date->format('s')
);
There's nothing built in that allows you to retrieve 'increments' however you can calculate it with minimal code. The use of modulo here allows you to figure out how far past the most recent 5 minute mark is. It will never be greater than 5 minutes (300 seconds) and can always be subtracted safely to take you back to the time you want.
$now = time();
echo $now . PHP_EOL;
$five_minutes = 60*5; // 300
$offset = $now % $five_minutes;
$five_block = $now - $offset;
echo date('Y-m-d H:i:s', $five_block);
First you will want to extract the minutes from their stored variable. Then mathematically this is simple by applying division and the floor function. To do this you can first divide by five using intdiv(numerator, denominator) which will remove any trailing decimal points and then multiply by five again to get your desired value at an increment of five.
Get the current time using time() :
$min = 300
$currentTime = time();
$mod = $currentTime % $min;
$res = $currentTime - $mod;
finalResult = date('Y-m-d H:i:s', $res);

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;

Convert String to time in PHP [duplicate]

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.

find the total hours in PHP by using two date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
I have two dates
$departure_Dtae=2012-07-25T07:30:00
$arrival_date =2012-07-25T10:25:00
T means Time i.e. in 2012-07-25T10:25:00 date is 2012-07-25 and the time is 10:25:00 (hr: mts: s)
I need to find out the total hrs between these two times
I.e. in this case the total hour is 2 hr and 55 minutes
But I don't know how I calculate this in PHP
Does anyone know this?
If using PHP Version 5.3+ then you can use DateTime::diff():
<?php
function getDiff($strStart, $strEnd) {
$start = DateTime::createFromFormat("Y-m-d G:i:s", $strStart);
$end = DateTime::createFromFormat("Y-m-d G:i:s", $strEnd);
return $start->diff($end)->format('%hhrs %imins and %sseconds ');
}
var_dump(getDiff('2012-07-25 07:30:00', '2012-07-25 10:25:00'));
its simple
$departure_Dtae="2012-07-25T07:30:00";
$arrival_date ="2012-07-25T10:25:00";
$departure_Dtae= str_replace("T", " ", $departure_Dtae);
$arrival_date= str_replace("T", " ", $arrival_date);
$diff= (strtotime($arrival_date)-strtotime($departure_Dtae));
echo date("h",$diff);
Try this :
$end_time = "2008-09-05 20:59:13";
$start_time = "2008-09-05 19:00:16";
$end = date("h:i:s",strtotime($end_time));
$start = date("h:i:s",strtotime($start_time));
$diff = strtotime($end) - strtotime($start);
//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff % 60;//seconds
//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes
$total_visit = $convert_hr.":".$remainder;
You can use the DateTime object in php, which has loads of methods for manipulating dates.
DateTime::createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php and DateTime::diff http://www.php.net/manual/en/datetime.diff.php would be the functions you would need to perform this task.
$date1 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 12:45');
$date2 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 13:45');
$interval = $date1->diff($date2);
echo $interval->format('H:i');
strtotime(date($arrival_date)) - strtotime(date($departure_date));
Will give you the time diff in secounds, then you can manipulate that as you wish.

Categories