please help me with strtotime . TQ in advance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zalikha
Forum Newbie
Posts: 2
Joined: Mon Dec 21, 2009 8:43 pm

please help me with strtotime . TQ in advance

Post by zalikha »

Hi all,
I have this function to calculate time different

Code: Select all

    function timeDif($hour1,$hour2){
        echo $hour1; echo "bb";
        echo $hour2; echo "bb";
        $dif = 0;       
        $hours[0] = date("H.i",strtotime($hour1));//"13:30:24"
//      $hours[0] = date("H.i",strtotime($input_start_hour));
        $hours[1] = date("H.i",strtotime($hour2));
//      $hours[1] = date("H.i",strtotime($input_end_hour));
        echo $hours[0]; echo "aa";
        echo $hours[1]; echo "aa";
        $hours[0] = ((int)$hours[0])*60 + ($hours[0]-((int)$hours[0]))*100;
        $hours[1] = ((int)$hours[1])*60 + ($hours[1]-((int)$hours[1]))*100;
        echo $hours[0]; echo "n";
        echo $hours[1]; echo "n";
        
        if($hours[0] > $hours[1]) {
            $hours[1] += 24*60;
        }
        $dif = sprintf("%.2f",($hours[1] - $hours[0]) / 60);
        echo $dif;
        return $dif;
    }
$total_hours = timeDif($input_start_hour,$input_end_hour);
the function works well but if the $hour2 is 24:00, $hours[1] will appear as 0730.
I really dont understand how it becomes 0730 instead of 2400 or 0000.

this is the output if i selected $hour1= 17:00, $hour2=24:00

17:0024:0017:00bb24:00bb17.00aa07.30aa1020n450n14.50


Please help me...
Thank you
jamesm6162
Forum Newbie
Posts: 13
Joined: Sat Jun 28, 2008 10:31 am

Re: please help me with strtotime . TQ in advance

Post by jamesm6162 »

Well technically there is no 24th hour. Time goes from 00h00 to 23h59 then back to zero. Well at least in our time system. So php most likely decides to use some default or even random value for an hour that it does not recognize.

Use var_dump(strtotime("24:00")) to see that strtotime returns false for that value.
zalikha
Forum Newbie
Posts: 2
Joined: Mon Dec 21, 2009 8:43 pm

Re: please help me with strtotime . TQ in advance

Post by zalikha »

Thank you jamesm6162.
I noticed that, it returned false.

I ve changed the time range to 00:00 - 23:59
and if ive selected 23:00 - 00:00, the time diff is counted properly which is 1 hr.

thanks againn..
Post Reply