Skip to content

Instantly share code, notes, and snippets.

@gbaldera
Forked from philsturgeon/gist:3216320
Created July 31, 2012 12:04
Show Gist options
  • Save gbaldera/3216508 to your computer and use it in GitHub Desktop.
Save gbaldera/3216508 to your computer and use it in GitHub Desktop.
Why DateTime Rocks
// Traditional
$parts = explode(' ', $dateTime);
$dates = explode('/', $parts[0]);
$times = explode(':', $parts[1]);
// check to see if it is am or pm
if(strtolower($parts[2]) == 'pm' && $times[0] != 12) {
// add 12 to the hour as it needs to be military time
$times[0]+=12;
}
return date('Y-m-d H:m:s', mktime($times[0], $times[1], $times[2], $dates[0], $dates[1], $dates[2]));
// With DateTime
return DateTime::createFromFormat("m/j/Y g:i:s A", $str)->format('Y-m-d H:i:s');
// Traditional Date difference
$parts = explode(' ', $start);
$dates = explode('/', $parts[0]);
$times = explode(':', $parts[1]);
$unix_start = mktime($times[0], $times[1], $times[2], $dates[0], $dates[1], $dates[2]);
$parts = explode(' ', $end);
$dates = explode('/', $parts[0]);
$times = explode(':', $parts[1]);
$unix_end = mktime($times[0], $times[1], $times[2], $dates[0], $dates[1], $dates[2]);
return ($unix_end - $unix_start);
// With Date Time
$objStart = DateTime::createFromFormat("m/j/Y g:i:s A", $strStart);
$objEnd = DateTime::createFromFormat("m/j/Y g:i:s A", $strEnd);
return $objStart->diff($objEnd)->format('%s');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment