How to compare two times using php [duplicate] - php

This question already has answers here:
How to compare two time in PHP
(11 answers)
Closed 8 years ago.
I have to compare time with a time value stored in a variable. I have devised the logic but it fails at time.
Following is the logic:
$diff = "22:32:53";
if ($diff > strtotime("072:00:00") )
{
echo "DO A";
}
else if ($diff < strtotime("08:00:00"))
{
echo "DO B";
}
else if( $diff > strtotime("08:00:00") && $diff < strtotime("072:00:00"))
{
echo "DO C";
}
But it echoes "DO A" whereas it should display "DO C". Could not figure out where I am missing the logic
Thanks in advance.

If you want to compare time (without date), the best way is to manually convert them into seconds, and compare the seconds. strtotime may work, but you'll need to run it on $diff too before comparing.

First thing, $diff is a string not an int, it should be:
$diff = strtotime("22:32:53");
Comparing is usually done using DateTime::diff
The comparison will not work anyway because if you check
var_dump(strtotime("072:00:00"));
will return:
false
strtotime is trying to match the format against a known time format, and 72 is not a valid hour.

Related

compare date string in php with mm-dd-yy format [duplicate]

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 5 years ago.
i need to compare two date from string,
my dates:
first date: 11-11-19
second date: 11-24-17
so i try to
$firstdate = "11-11-19";
$seconddate = "11-24-17";
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
if i change < or > the if statement should change, but i get always the firsdate...
how to do to compare two dates in this forma mm-dd-yy ?
Thanks
You can use strtotime to convert string to unix timestamp, and compare that.
$firstdate = strtotime("11-11-19");
$seconddate = strtotime("11-24-17");
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
You can convert both dates to timestamp:
echo (strtotime($firstdate) < strtotime($seconddate)) ? "firstdate is minor than the secondate" : "seconddate is major than the firstdate";

Can date() be compared like this? [duplicate]

This question already has answers here:
Trying to define my own time type in PHP
(2 answers)
Closed 6 years ago.
I have had some issues with comparing date/times.
I had a working code that just stopped working all of a sudden. Now I made some changes to it and by some reason UNIX time is not correct.
Utime and nexttime is just an example string now, but in the code Utime is last time updated, and nexttime is next time a update will happen calculated from Utime.
Example:
$Utime = "201603300450"; // YmdHi 2016 03 30 04:50
$nexttime = "201603300520";
if (date("YmdHi")>=$Utime && date("YmdHi")>=$nexttime)
Is this a safe way to compare the timevalue? I had some issues with strtotime thats why I ask.
You should convert your dates to DateTime objects:
<?php
$Utime = "201603300450";
$nexttime = "201603300520";
$dateUtime = DateTime::createFromFormat('YmdHi', $Utime);
$datenexttime = DateTime::createFromFormat('YmdHi', $nexttime);
$nowDate = DateTime::createFromFormat('YmdHi', date('YmdHi'));
if ($nowDate>=$dateUtime && $nowDate>=$datenexttime)
//do your stuff...
Strtotime should work just fine, but you should stick to a valid format. "Y-m-d H:i:s" is a valid format for strtotime.
Hope it helps :)
strtotime() is a great helper in this regard.
Check out strtotime
$Utime = "201603300450";
$nexttime = "201603300520";
if ( strtotime('now') >= strtotime($Utime) && strtotime('now') >= strtotime($nexttime) )

PHP if condition based on date not working [duplicate]

This question already has answers here:
Compare given date with today
(14 answers)
Closed 8 years ago.
I want to execute a if statement where I want if given {date} is less or equal than today then {due_amount}= {paidbystudent} else {due_amount}= '0'
Here is my code:
if ({date} <= Today()) {
{due_amount}={paidbystudent};
} else {
{due_amount}='0';
}
But its not working. Please help me how to do this.
I have also tried this code but its not working properly this is checking only date and ignoring month and year
$todays_date=date("d/m/Y", strtotime("today"));
$date=date("d/m/Y", strtotime({date}));
if ($date <= $todays_date) {
{due_amount}={paidbystudent};
} else {
{due_amount}='0';
}
How about this:
$date = '2014-05-22'; // fetch from db, user input etc
if (strtotime($date) <= time()) {
echo 'paid by student';
} else {
echo 0;
}
DEMO
EDIT: As pointed out by deceze, a better way to approach this would be to compare the UNIX timestamp values instead of a date format.

Adding two DateTime objects in php [duplicate]

This question already has answers here:
How we can add two date intervals in PHP
(4 answers)
Closed 9 years ago.
I have two DateTime object.
$time1 = new DateTime('01:04:00');
$time2 = new DateTime('00:13:22');
Addition of this two will be : 01:17:22. How can I do it?
A "time of day" is not the same thing as a "duration of time". It doesn't makes sense to add together two time of day values - regardless of platform or language. Think about it - what does "11:00 PM" + "4:00 AM" equal? It's a nonsensical question.
You should be thinking about PHP's DateInterval class, not the DateTime class.
It should be noted that if you follow the examples on the dup posts of using strtotime it will work only when each individual input, and the final result, are all under 24 hours. Why? Because that's the maximum amount of time allowed in a standard day. That's the consequence of mixing "time of day" with "duration of time".
This should work for you:
function time_to_interval($time) {
$parts = explode(':',$time);
return new DateInterval('PT'.$parts[0].'H'.$parts[1].'M'.$parts[2].'S');
}
function add_intervals($a,$b) {
$zerodate = new DateTime('0000-01-01 00:00:00');
$dt = clone $zerodate;
$dt->add($a);
$dt->add($b);
return $zerodate->diff($dt);
}
function format_interval_hhmmss($interval){
$totalhours = $interval->h + ($interval->d * 24);
return $totalhours.$interval->format(':%I:%S');
}
$interval1 = time_to_interval('01:04:00');
$interval2 = time_to_interval('00:13:22');
$interval3 = add_intervals($interval1,$interval2);
echo format_interval_hhmmss($interval3);
Note that the choice of value for $zerodate isn't really all that important. It's just that some reference point is required, since PHP doesn't provide operations directly on DateInterval.
Also note that the the DateInterval::format function doesn't have a formatter to get you total number of hours inclusive of days, so if there's any chance the total could be 24 hours or more, then you have to format that part yourself, like I showed in the format_interval_hhmmss function.
Also note that my PHP skills are not all that great, so there may be a more efficient way to write these functions.
function addtime($time1,$time2)
{
$x = new DateTime($time1);
$y = new DateTime($time2);
$interval1 = $x->diff(new DateTime('00:00:00')) ;
$interval2 = $y->diff(new DateTime('00:00:00')) ;
$e = new DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
$total = $f->diff($e)->format("%H:%I:%S");
return $total;
}
The only built-in DateTime add/substract methods require using a DateInterval. e.g
$t1 = new DateTime('01:04:33');
$new = $t1->add(new DateInterval('PT13M22S'));
^^^^^^---13 minutes, 22 seconds
however, note that since DateTime works on DATES as well as times, you can't just slam together two times like this and expect to get reliable results. Consider what happens if you're doing the addition on an interval that happens to span a daylight savings border, or crosses over a day boundary, etc...

PHP - How to find if a date is between 2 dates [duplicate]

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 9 years ago.
I get some datas with dates from a database :
$data['my_date'])
Type is date : YYYY-MM-DD
I would like to make with PHP a condition to found if $data['my_date']) is between 2 dates like :
if ($data['my_date'] >= 2009-01-01 && $data['my_date'] <= 2009-12-31) {
$datas = '5';
}
else {
$datas = 1;
}
It doesn't work as expected, the condition is not verified. What should i do ?
There is no literal syntax for dates, you should enclose them in quotes and treat them as strings. If you are using the YYYY-MM-DD format, then it is sortable alphabetically, and your current method should work.
if ($data['my_date'] >= '2009-01-01' && $data['my_date'] <= '2009-12-31') {
$datas = '5';
}
else {
$datas = 1;
}
If you don't quote your dates, you are doing integer operations:
$data['my_date'] >= 2009-01-01 ====> $data['my_date'] >= 2007
You need to convert all dates by strtotime() to timestamp and then compare them.
strtotime($data['my_date']) >= strtotime('2009-01-01')...
You use either strtotime() or mktime() to convert your dates into timestamp, then compare it.

Categories