user2226714
user2226714

Reputation: 73

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

Checking a $datetest array to find out if set dates are between the first and last day of current month. Output is wrong. dates, such as, '11-08-2014', '11-8-2014', are right and wrong in the output.

//get First day of Month
//get Last day of Month

$first_day_of_month = date('m-01-Y');

$last_day_of_month = date('m-t-Y');

echo "First Day of Month: ";
echo $first_day_of_month;

echo "<br />";

echo "Last Day of Month: ";
echo $last_day_of_month;

$datetest = array('11-30-2014', '11-31-2014', '11-08-2014', '11-8-2014', '11-9-2014', '11-11-2014', '11-24-2014', '11-1-2014', '10-30-2014', '12-1-2014', '11-1-1997');

echo "<br /><br />";

foreach ($datetest as $datetest) {  
if (($datetest >= $first_day_of_month) && ($datetest <= $last_day_of_month)){

    echo "$datetest :";
    echo " Is in Month!";
    echo "<br />";

} else {
    echo "$datetest :";
    echo "Not in Month!";
    echo "<br />";
}
}

Outputs: 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 Month!

11-08-2014 : Is in Month!

11-8-2014 :Not in Month! X

11-9-2014 :Not in Month! X

11-11-2014 : Is in Month!

11-24-2014 : Is in Month!

11-1-2014 : Is in Month!

10-30-2014 :Not in Month!

12-1-2014 :Not in Month!

11-1-1997 : Is in Month! X

Upvotes: 0

Views: 9639

Answers (5)

Xpacer
Xpacer

Reputation: 41

You can split the comparison by comparing with the month of the year, then perform a valid day check. If you won't have issues like 32-08-2014 or 0-08-2014, you can leave the day check.

 public function isWithinCurrentMonth($date)
 {
    $current_month = date('Y-m');
    $transaction_date_month = date('Y-m', strtotime($date));
    $transaction_day = $date('d', strtotime($date));
    $last_day = $date('t');

    return $current_month == $transaction_date_month
         // day validity check
        && intval($transaction_day) > 0
        && intval($transaction_day) <= intval($last_day);
 }

Upvotes: 0

Kypros
Kypros

Reputation: 2986

As already @mark91 has told you, the comparison you are doing right now is wrong since you are practically comparing strings against string and not actual dates:

The best (if possible) way to fix your problem would be to first change your date format to YYYY-mm-dd and then use strtotime() before comparing the values.

If changing the values is not an option you could use a combination of mktime to "build" your dates (as numbers) and explode() to convert "your" format to proper time format as in the sample code i tried for you

Test Date Comparisons

Note I am building the start and end dates using mktime() with a 1 (first day) and a date('n') (number of days in month = equal to last day of month). For more info check date() function

The script inside the loop, breaks (explode) up the poorly formatted date into an array, and then mktime() generates the date back into a numeric date representation (unix timestamp) which can be used for easy and correct comparison of the values.


Important Parts:

Build first and last dates using:

$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')); 

Split and build the date for comparison:

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

Compare the unix timestamps instead of the strings:

if (($comp_date >= $first_day_of_month) && ($comp_date <= $last_day_of_month)){

Upvotes: 3

Jaime
Jaime

Reputation: 1410

$first_day_of_month = strtotime(date('Y-m-01'));
$last_day_of_month = strtotime(date('Y-m-t 23:59:59'));

echo "First Day of Month: ";
echo $first_day_of_month;

echo "<br />";

echo "Last Day of Month: ";
echo $last_day_of_month;

$datetests = array('2014-11-30', '2014-11-31', '2014-11-08');
echo "<br /><br />";

foreach ($datetests as $datetest) {  
if ((strtotime($datetest) >= $first_day_of_month) && (strtotime($datetest) <= $last_day_of_month)){

    echo "$datetest :";
    echo " Is in Month!";
    echo "<br />";

} else {
    echo "$datetest :";
    echo "Not in Month!";
    echo "<br />";
}
}

Upvotes: 3

mgaido
mgaido

Reputation: 3055

The problem with your code is that you are not comparing dates, but strings representing them. If you want to do so, you have to re-format your dates (in order to have formats like YYYY-mm-dd) or, better, you just need to convert them in timestamps and compare them (you can do this with mktime).

Upvotes: 3

JMc
JMc

Reputation: 355

It's because date() returns a string. Convert the string to an int using intval()

// gets the year (as an integer)
$date = intval(date('Y'));

Integer comparisons are much easier than string comparsions.

Upvotes: -1

Related Questions