Skip to content

Instantly share code, notes, and snippets.

@andho
Created November 30, 2012 12:47
Show Gist options
  • Save andho/4175558 to your computer and use it in GitHub Desktop.
Save andho/4175558 to your computer and use it in GitHub Desktop.
Kindah rounds off dates
<?php
$defaults = array(
'month' => 'january',
'day' => 1,
'hour' => 0,
'minute' => 0,
'second' => 0
);
$givenunit = 'month';
$date = '4 Apr 12 23:38:03';
$conv = date('Y-m-d H:i:s', strtotime($date));
//$conv = '2012-04-04 23:38:03';
list($d, $t) = explode(' ', $conv);
$arr = array_merge(explode('-', $d), explode(':', $t));
$newdate = array(array_shift($arr));
$unitreached = false;
foreach ($defaults as $unit => $default) {
if ($unitreached) {
$newdate[] = $default;
continue;
}
if (!$unitreached && $unit == $givenunit) {
$unitreached = true;
}
$newdate[] = array_shift($arr);
}
echo implode(' ', $newdate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment