[Phpwm] days_between()
Paul Matthews
paul.matthews.86 at gmail.com
Thu Jun 7 16:33:19 BST 2007
On 07/06/07, Phil Beynon <phil at infolinkelectronics.co.uk> wrote:
>
> > > This might come in useful to someone, it takes a mysql datetime
> > field and
> > > tells you how many days between it and either another datetime field
> or
> > > formatted date() YYYY-MM-DD HH:MM:SS in a signed manner.
> > > Its pretty flexible about data input and will return an X if it doesnt
> > > like
> > > incoming data.
> >
> >
> > Sounds like a good idea, but the idea of using 'return "x"' as an
> > error is, in my mind, a really bad idea. if you were using PHP4 I'd
> > have returned 'NULL' or something that made more sense, if you were
> > using PHP5 then I'd have thrown an exception of some kind.
>
> It was just the way it fitted in with something else:-
>
> $days = days_between($date1, $date2);
> if($days == 'X'){echo "illegal date data";} else {
> if($days < 0 ){echo "ended " . abs($days) . " days ago<br>";}
> if($days == 0 ){echo "ending " . abs($days) . " today<br>";}
> if($days > 0 ){echo "will end in " . abs($days) . " days time<br>";}
> }
>
> That was all, modify it as you feel fit! :-)
>
> > function days_between($a,$b){
> > > $aa = explode(" ",$a); $ab = explode("-",$aa[0]);
> > > $ba = explode(" ",$b); $bb = explode("-",$ba[0]);
> > > if(@checkdate($ab[1],$ab[2],$ab[0]) AND
> > @checkdate($bb[1],$bb[2],$bb[0])){
> > > $a_bits = getdate(strtotime($a));
> > > $b_bits = getdate(strtotime($b));
> > > $a_new =
> mktime(12,0,0,$a_bits['mon'],$a_bits['mday'],$a_bits['year']);
> > > $b_new =
> mktime(12,0,0,$b_bits['mon'],$b_bits['mday'],$b_bits['year']);
> > > if($a_new > $b_new){return 0 - round(abs($a_new - $b_new)/86400);}
> > > return round(abs($a_new - $b_new)/86400);
> > > } else return "X";
> > > }
> >
> >
> > The other thing that I'm lead to believe is bad coding practise is the
> use
> > of the '@' symbol. I can understand not wanting to see the errors, but
> > surely you should be catching them and dealing with them or let
> > them bubble
> > out so that the layer above can catch them.
> > Finally perhaps a more suitable naming convention for your variables
> might
> > assist other programmers and readability in the future.
>
> Huh? thats the way it returns the 'X' - if you don't suppress the error
> that
> way it breaks the page by dumping the error out. I didnt want that
> happening, if it has an error then its going to trigger the 'else return
> "X"' caused by the boolean off the 'if' - try it and see! :-)
I think you're missing the point I'm trying to make (and yes I was
wrong in suggesting using try and catch due to PHP's internal error
handling, but that's due to my java background that I expect to be
able to catch any runtime error thrown) .
The point I'm trying to make is that 'x' is a bad error character to pass
back. Surely the 'null' character would make more sense as that way the view
(or whatever is calling the function) could deal with the result as it saw
fit? I personally would even stay clear of using Boolean false because of
php's dynamic typing. As for the use of the '@' symbol, I've always been led
to believe that you should try not to use it where possible. I would have
thought a better way is to check the data being passed into the function
rather than allow PHP to have an error that you would suppress?
Also for code readability, would it not be better to keep to 'good
practises' and not use single line if statements?
I hope that clears up my point.
>From Paul M.
More information about the Phpwm
mailing list