Skip to content

Instantly share code, notes, and snippets.

@coreymcmahon
Created March 7, 2014 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreymcmahon/9406896 to your computer and use it in GitHub Desktop.
Save coreymcmahon/9406896 to your computer and use it in GitHub Desktop.
TestDateTime
<?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