Created
December 9, 2014 23:58
-
-
Save rvalenciano/c00f8b15e7c9c8e935a1 to your computer and use it in GitHub Desktop.
Problem of counting the number of days since the beginning of the year
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 | |
function countDays($dateInString) { | |
// Write your code here | |
// To print results to the standard output you can use print | |
// Example: | |
// print "Hello world!"; | |
$month = ''; | |
$day = ''; | |
$year = ''; | |
list($month, $day, $year) = split('[/.-]', $dateInString); | |
if (checkdate($month, $day, $year)) { | |
// valid date ... | |
//print "Month: $month; Day: $day; Year: $year<br />\n"; | |
$startTimeStamp = strtotime($year . "/01/01"); | |
$endTimeStamp = strtotime($year . "/" . $month . "/" . $day); | |
$timeDiff = abs($endTimeStamp - $startTimeStamp); | |
$numberDays = $timeDiff/86400; // 86400 seconds in one day | |
// and you might want to convert to integer | |
$numberDays = intval($numberDays); | |
print $numberDays + 1; | |
} | |
else | |
{ | |
print "Bad format"; | |
} | |
} | |
// Do NOT call the countDays function in the code | |
// you write. The system will call it automatically. | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment