Last active
August 29, 2015 14:06
-
-
Save gtwalford/9e023c7e4a2dae5a5808 to your computer and use it in GitHub Desktop.
Php Date Time Comparisons
This file contains 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
// Set Expiration DateTime | |
$expiresDate = new DateTime( $data['expires'], new DateTimeZone( 'UTC' ) ); | |
// Set Current DateTime | |
$now = new DateTime('now', new DateTimeZone( 'UTC' )); | |
// If needed determine difference | |
$dateDiff = $expiresDate->diff( $now ); | |
// Print out for testing | |
echo "======================================================\n"; | |
echo "Expiration DateTime = ".$expiresDate->format('Y-m-d H:i:s')."\n"; | |
echo "Current DateTime = ".$now->format('Y-m-d H:i:s')."\n"; | |
echo "The difference is ---" | |
echo "Years: {$dateDiff->y }\n"; | |
echo "Months: {$dateDiff->m }\n"; | |
echo "Days: {$dateDiff->d }\n"; | |
echo "Hours: {$dateDiff->h }\n"; | |
echo "Mins: {$dateDiff->i }\n"; | |
echo "Secs: {$dateDiff->s }\n"; | |
echo $dateDiff->format("%Y years, %m months, %d days, %H hours, %i minutes, %s seconds") . "\n"; | |
echo "Check which date is greater ---\n"; | |
// Will print out 1 if true | |
echo "Expiration is greater than now = ".( $expiresDate > $now )."\n"; | |
echo "Expiration is less than now = ".( $expiresDate < $now )."\n"; | |
echo "======================================================\n"; | |
// Conditional if current time is great than expiration | |
if( $expiresDate < $now ){ | |
// Do stuff | |
} | |
else { | |
// Do other stuff if current time is less than expiration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment