23284

Why isn't 03 bigger than 00?

Question:

With this IF statement:

if ( (date('i')>=00) && (date('i')<=19) ) { $a=1; } if ( (date('i')>=20) && (date('i')<=39) ) { $a=2; } if ( (date('i')>=40) && (date('i')<=59) ) { $a=3; } else { $a=0; }

When date('i') is for example "03", I'm getting a=0 instead of a=1.

Shouldn't 03 be considered bigger than 00?

What's the best way to achieve what I want here?

Answer1:

It's got nothing to do with variable types.

The last if statement makes your first two irrelevant. $a will only ever be either 3 or 0. You need else if.

Also, assign date('i') to a variable to avoid calling it constantly.

$d = date('i'); if ($d >= 00 && $d <= 19) { $a = 1; } else if ($d >= 20 && $d <= 39) { $a = 2; } else if ($d >= 40 && $d <= 59) { $a = 3; } else { $a = 0; }

Or, to make things neater:

$d = date('i'); $a = 0; if($d >= 00) $a = 1; if($d >= 20) $a = 2; if($d >= 40) $a = 3;

Answer2:

In PHP the function date() return a string, so the best way to compare date with integer is to do the casting. Try with this syntaxe : if((int)date('i') > 0 && (int)date('i') <= 19) { $a = 1; }

Best Regards, Anis

Recommend

  • how can i do UIView animateWithDuration in viewDidLoad? ios7
  • With knitr, preserve chunk options when purling chunks into separate files
  • Is Android development possible on netbooks? [closed]
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • Selection Sort, For Java
  • Generate a unique string based on a pair of strings
  • Mocha throws unexpected token error for ES6 object spread operator
  • R sqldf renaming a field in a select statement
  • EntLib Way to Bind “Null” Value to Parameter
  • Criterion causing memory consumption to explode, no CAFs in sight
  • .NET video play library which allows to change the playback rate?
  • Is it possible to define rest argument in OCaml?
  • What causes the runtime difference in this trivial fortran code?
  • How to 'create temp table as select' in Slick?
  • Cast between interfaces whose interface signatures are same
  • Django model inheritance, filtering models
  • uniform generation of points on 3D box
  • MySQL Order by column = x, column asc?
  • Assign variable to the value in HTML
  • java inputstream
  • C++ Partial template specialization - design simplification
  • Illegal mix of collations for operation for date/time comparison
  • Javascript simulate pressing enter in input box
  • Is possible to count alias result on mysql
  • Redux, normalised entities and lodash merge
  • Release, debug version and Authorization Google?
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Numpy divide by zero. Why?
  • How to format a variable of double type
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Benchmarking RAM performance - UWP and C#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?