How to check time correctly?

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
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

How to check time correctly?

Post by 9902468 »

I'm using this function to check time:

Code: Select all

//function that checks time syntax (hh.mm), correct values etc..
                        function check_time($time){

                                $return = 0;

                                if( ereg("(ї0-9]{1,2})\.(ї0-9]{1,2})", $time) ){

                                        $time_info = explode(".", $time);

                                        if($time_info&#1111;0] < 24 && $time_info&#1111;0] >= 0 && $time_info&#1111;1] < 60 && $time_info&#1111;1] >= 0)&#123;

                                                $return = 1;
                                        &#125;else&#123;
                                                $return = 0;
                                        &#125;

                                &#125;else&#123;
                                        $return = 0;
                                &#125;

                        return $return;

                        &#125;
But it returns 1 if I input time that has many zeros like 0002.22. I don't want that to be ok. (Don't ask why ;) ) Is there something I'm missing?

thanks

-9902468
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

http://www.time.gov usually has the right time.. ;)
Image Image
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

OK if your time is in this format

Code: Select all

hh:mm
Use this reg expression

Code: Select all

&lt;?php
ereg("^&#1111;0-9]{2}:&#1111;0-9]{2}");
?&gt;
Post Reply