Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Created October 28, 2016 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save calebporzio/ee3ea3b805f56793a0865282f237ed1f to your computer and use it in GitHub Desktop.
Save calebporzio/ee3ea3b805f56793a0865282f237ed1f to your computer and use it in GitHub Desktop.
<?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