SureshKumar Vegesna
SureshKumar Vegesna

Reputation: 1202

date time comparison in php

i have two time values as give below

$row2[0]=00:40:00;
$row1[5]=14:00:00;
$time=14:33:00
$time=$row[1];

i am combining $row2[0],$row1[5] like below

$secs = strtotime($row2[0])-strtotime("00:00:00");
$result = date("H:i:s",strtotime($row1[5])+$secs);

$result=14:40:00

i use if condition as shown

if($row[1]>$row1[5] && $row[1]<$result)
{
$message=array("status"=>$result);
}
else
{
$message=array("status"=>'');
}

but i get "status"=>"" its not correct

i want to get "status"=>"14:40:00"

please help us for getting correct out put

Upvotes: 0

Views: 260

Answers (3)

sikas
sikas

Reputation: 5523

I go with Panique on his answer, I want to add also:

You declared two vars:

$row2[0]=00:40:00;
$row1[5]=14:00:00;

But you called different array elements in the if

if($row[1]>$row1[5] && $row[1]<$result)

Solve this and you should have your code working.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

You don't appear to define $row[1] anywhere, so it is treated as NULL, which as an integer is 0, which cannot be greater than the time you have given in $row1[5].

Try using less ambiguous variable names, it might make it easier to spot problems like this.

Personally I don't see why that if is there at all, just remove it and the else block, leaving just $message = array("status"=>$result);.

Upvotes: 2

Sliq
Sliq

Reputation: 16494

is this real oder pseudo code ? because

$row2[0]=00:40:00;

is not real php code !

$row2[0] = 00:40:00; // $row2[0] would be 0, because you are assigning an integer
$row2[0] = "00:40:00"; // $row2[0] would be "00:40:00", but as a string (!)

i would always work with timestamps btw.

Upvotes: 1

Related Questions