Created
November 30, 2012 12:47
-
-
Save andho/4175558 to your computer and use it in GitHub Desktop.
Kindah rounds off dates
This file contains hidden or 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 | |
$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