PHP check if date is between first and last day of current month -


The

The first and last day of what has been investigating $ $ datetest array to determine the set dates for the current month Are in between. Output is incorrect Date, '11 -08-2014 ', '11-8-2014', is correct and inaccurate in the output.

  // Get the first day of the month / get the last day of month $ first_day_of_month = date ('m-01-y'); $ Last_day_of_month = date ('m-t-y'); Echo "First Day of the Month:"; Echo $ first_day_of_month; Echo "
"; Echo "Last day of the month:"; Echo $ last_day_of_month; $ Datetest = array ('11 -30-2014 ', '11 -31-2014', '11 -08-2014 ', '11 -8-2014, -9-2014 '11, '11 -11- 2014 ', '11 -24-2014', '11-1-2014 ', '10 -30-2014', '12 -1-2014 ', '11-1-1997'); Echo "









< Forex ($ datetest $ $ datetest) {if (($ datetest & gt; = $ first_day_of_month) & amp; amp; amp; amp; amp; & amp; amp; amp; amp; amp; dollar ($ datetest & lt ; = $ Last_day_of_month)) {echo "$ datetest:"; The echo is "in the month!"; Echo "
"; } Else {resonant "$ datetest:"; Echo "not in the month!"; Echo "
"; }}

Output: First day of month: 11-01-2014 Last day of month: 11-30-2014

11-30-2014: is in Month!

11-31-2014: Not in the month!

11-08-2014: In the month!

11-8-2014: Not in the month! X

11-9-2014: Not in the month! X

11-11-2014: In the month!

11-24-2014: In the month!

11-1-2014: is in the month!

10-30-2014: Not in the month!

12-1-2014: Not in the Month!

11-1-1997: is in the month! X

As already mentioned by @ Mark 91, you're comparing the so comparisons are not the wrong practically string s string and compares the actual dates:

best (If possible) How to fix your problem First, change your date format to YYYY-mm-dd and then before comparing values, strtotime () .

If changing prices is not an option you can use to combine the mktime to create your code as "time" and explode () to "for your sample code to convert the appropriate time format your" format I tried

Note: I mktime () with a 1 (the first day) and a date ('n') (number of months in months = on the last day of the month Abber). For more information check out

loop in the script, bad format in an array of went on to break ( Blast ) and mktime () generates the date in a numerical date representation (Unix timestamp) which can be used in comparison to the simple and correct values. Make a date using


Important part:

first and the last :

  $ first_day_of_month = mktime (0,0,0, date ('n'), 1, date ('Y')); $ Last_day_of_month = mktime (23, 59, 59, date ('n'), date ('t'), date ('y'));  

Build date for split and comparison:

  $ split_date = explosion ("-", $ datetest); $ Comp_date = mktime (0,0,0, $ split_date [0], $ split_date [1], $ split_date [2]);  

Compare Unix timestamps instead of strings:

  if ((comp_date = $ first_day_of_month) & amp; amp; amp ; Amp; amp; amp; = $ Last_day_of_month)) { 

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -