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

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

Related

Compare two hours inside while loop [duplicate]

This question already has answers here:
Print time in 15-minute increments between two times in the same day
(10 answers)
Closed 11 months ago.
I'm trying to echo list of hours.
<?php
$startTime = "9:00:00";
$endTime = "19:00:00";
$st = strtotime($startTime);
$et = strtotime($endTime);
while ($st < $et) {
echo $st = date( "H:i", strtotime('+30 minutes', $st) );
echo '<br>';
}
?>
Inside that list, users will choose their reservation hour. But my while loop works like infinite loop.
Just like the previous answer, you're messing with data types while doing arithmetic.
PHP ships with built-in date time arithmetic through date and time API.
Your code could look much more clear and elegant and less error prone (no strtotime(), conversion, etc)
You could do something like this instead:
<?php
$startTime = DateTime::createFromFormat('H:i:s', '9:00:00');
$endTime = DateTimeImmutable::createFromFormat('H:i:s', '19:00:00');
$interval = new DateInterval('PT30M'); // 30 minutes
$reservation = [];
while ($startTime <= $endTime) {
$reservation[] = $startTime->format('H:i');
$startTime->add($interval);
}
echo join(PHP_EOL, $reservation) . PHP_EOL;
Links:
Date/Time Arithmetic
You're converting $st from a integer (timestamp) with strtotime() to a string with date() so your loop keeps going since while (string < integer)... is always true.
You can do this instead:
while ($st < $et) {
$st += (30 * 60); // Add 30 minutes in seconds
echo date("H:i", $st);
echo '<br>';
}

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

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

PHP check if pass 5 minutes between two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I am trying to check if pass 5 minutes between two dates.
Here is my code:
$time = strtotime("+5 minutes").'<br>';
$var = '1518219956';
$e1 = date("d-m-Y H:i",$time);
$e2 = date("d-m-Y H:i",$var);
if($var > $time){
echo 'true<br>';
echo 'Time: '.$e1.'<br> Var:'.$e2;
} else{
echo 'false<br>';
echo 'Time: '.$e1.'<br> Var: '.$e2;
}
I am sorry but I lost myself with this timestamps..
Sorry, I don't mean to be rude but I was confused by the question. What I designed here was to see if time B is five minutes after Time A.
A few notes:
No need to bother with strtotime or date. Just keep everything in unix time. Compare the seconds by using 60 * 5. 60 for 60 seconds in a minute and 5 for the number of minutes.
<?php
//Time A
$timeA = '1518223062';
//Time B (Which I set at the current time)
$timeB = time();
//five minutes in seconds
$fiveMinutes = 60 * 5;
//check if current time is after 5 minutes the initial time
if ( ($timeA+$fiveMinutes) <= $timeB) {
echo "True";
}
else {
echo "False";
}
?>

strtotime() seems bugged with a special date [duplicate]

This question already has answers here:
How do I add 24 hours to a unix timestamp in php?
(7 answers)
Closed 8 years ago.
I'm writing a function which calculates the days between two dates.
Strangely I have a strange behaviour for one special day. Here is a part of my code:
$startTimestamp = strtotime('25-10-2014');
$endTimestamp = strtotime('28-10-2014');
for($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24))
{
echo date("d-m-Y", $i).'<br />';
}
This example gives me this result:
25-10-2014
26-10-2014
26-10-2014
27-10-2014
I don't know why the date "26-10-2014" appears two times.
If I try with another dates like:
$startTimestamp = strtotime('25-11-2014');
$endTimestamp = strtotime('28-11-2014');
The result is correct:
25-11-2014
26-11-2014
27-11-2014
28-11-2014
It is probably because of the DST (daylight saving time) in the timezone your PHP is configured to.
This mean, if you jump forward 24h, you'll still be the same day, because the day is 25h long.
I would use the DateTime class:
$startTime = new DateTime('25-10-2014');
$endTime = new DateTime('28-10-2014');
do {
echo $startTime->format('d-m-Y'), PHP_EOL;
} while($startTime->modify('+1 day') <= $endTime);
Note that you can use the <= operator when comparing DateTime objects. Also the DateTime::modify() method is aware of daylight saving time issues. (Note that there is a change in daylight saving time on Oct 26)

Getting time difference between two times in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get time difference in minutes in PHP
I am working on an attendance table to calculate the late and very late employees.
I am storing the login time in the table (Type: time). I am able to get the time from the database and i would like to show the time difference in the separate column.
i.e., if employee logged on or before 09:00:59, then its right time and the time diff should be shown as null. If he logs in after the time i.e, 09:01:00 or later the time difference should be 00:00:01. Like wise i need to calculate the differences in time.
One time is constant i.e., 09:00:59 and another one i am getting from database table. Need to get diff between both.
I am working in PHP.
Hope my question is clear.
Thank you in Advance.
You can use strtotime() for time calculation. Here is an example:
$checkTime = strtotime('09:00:59');
echo 'Check Time : '.date('H:i:s', $checkTime);
echo '<hr>';
$loginTime = strtotime('09:01:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!'; echo '<br>';
echo 'Time diff in sec: '.abs($diff);
echo '<hr>';
$loginTime = strtotime('09:00:59');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';
echo '<hr>';
$loginTime = strtotime('09:00:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';
Demo
Check the already-asked question - how to get time difference in minutes:
Subtract the past-most one from the future-most one and divide by 60.
Times are done in unix format so they're just a big number showing the
number of seconds from January 1 1970 00:00:00 GMT
You can also use DateTime class:
$time1 = new DateTime('09:00:59');
$time2 = new DateTime('09:01:00');
$interval = $time1->diff($time2);
echo $interval->format('%s second(s)');
Result:
1 second(s)
<?php
$start = strtotime("12:00");
$end = // Run query to get datetime value from db
$elapsed = $end - $start;
echo date("H:i", $elapsed);
?>

Categories