Created
October 28, 2016 18:58
-
-
Save calebporzio/ee3ea3b805f56793a0865282f237ed1f to your computer and use it in GitHub Desktop.
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
<?php | |
// Before: | |
function getStartDate() { | |
return preg_match("/^\d\d\d\d-\d\d-\d\d$/", $_GET['dStartDate']) ? $_GET['dStartDate'] : date("Y-m-d", time() - 60 * 60 * 24 * 7); | |
} | |
function getEndDate() { | |
return preg_match("/^\d\d\d\d-\d\d-\d\d$/", $_GET['dEndDate']) ? $_GET['dEndDate'] : date("Y-m-d"); | |
} | |
// After: | |
function getStartDate() { | |
return normalizedDate($_GET['dStartDate'], time() - 60 * 60 * 24 * 7); | |
} | |
function getEndDate() { | |
return normalizedDate($_GET['dEndDate'], time()); | |
} | |
function normalizedDate($dateString, $defaultTimestamp) { | |
return isProperlyFormattedDate($dateString) ? $dateString : formatDate($defaultTimestamp); | |
} | |
function isProperlyFormattedDate($dateString) { | |
return preg_match("/^\d\d\d\d-\d\d-\d\d$/", $dateString); | |
} | |
function formatDate($timestamp) { | |
return date("Y-m-d", $timestamp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment