Created
March 7, 2014 07:19
-
-
Save coreymcmahon/9406896 to your computer and use it in GitHub Desktop.
TestDateTime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* function to check whether a date is within the last 30 days | |
*/ | |
function withinOneMonth ($checkDate) { | |
$now = new DateTime(); | |
$thirtyDayInterval = new DateInterval('P30D'); | |
/* create a date 30 days in the past */ | |
$before = $now->sub($thirtyDayInterval); | |
return ($checkDate > $before); | |
} | |
/* today's date is 7th of March 2014 */ | |
/* 7 days ago, 28th of February */ | |
echo (withinOneMonth(new DateTime('2014-02-28 00:00:00+11:00'))) ? | |
'within 30 days' : 'NOT within 30 days'; | |
// echos 'within 30 days' | |
/* 29 days ago, 6th of February */ | |
echo (withinOneMonth(new DateTime('2014-02-06 00:00:00+11:00'))) ? | |
'within 30 days' : 'NOT within 30 days'; | |
// echos 'within 30 days' | |
/* 31 days ago, 4th of February */ | |
echo (withinOneMonth(new DateTime('2014-02-04 00:00:00+11:00'))) ? | |
'within 30 days' : 'NOT within 30 days'; | |
// echos 'NOT within 30 days' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment